-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AbstractModelTest and AbstractEnumTest classes for model and enum…
…eration testing. This slightly simplifies creating the model and enum tests by providing otherwise redundant aspects in an abstract class.
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.tdl.vireo.model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@ActiveProfiles("test") | ||
@ExtendWith(MockitoExtension.class) | ||
public abstract class AbstractEnumTest<E extends Enum<?>> { | ||
|
||
public static final String NAME_MESSAGE = "The enumeration name is different than expected."; | ||
public static final String ORDINAL_MESSAGE = "The enumeration ordinal is different than expected."; | ||
|
||
/** | ||
* Perform the test for the given enum. | ||
* | ||
* The implementing class must implement the provideEnumParameters() static method, returning Stream<Arguments>. | ||
* | ||
* @param enumeration The enumeration to test. | ||
* @param name The expected enumeration name value. | ||
* @param ordinal The ordinal of the enumeration. | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource("provideEnumParameters") | ||
public void testEnum(Enum<?> enumeration, String name, int ordinal) { | ||
assertEquals(enumeration.name(), name, NAME_MESSAGE); | ||
assertEquals(enumeration.ordinal(), ordinal, ORDINAL_MESSAGE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.tdl.vireo.model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
@ActiveProfiles("test") | ||
@ExtendWith(MockitoExtension.class) | ||
public abstract class AbstractModelTest<M> { | ||
|
||
public static final String GETTER_MESSAGE = "The getter must return the correct response."; | ||
public static final String SETTER_MESSAGE = "The setter must assign the correct value."; | ||
|
||
/** | ||
* Perform the test for the given getter. | ||
* | ||
* The implementing class must implement the provideGetterParameters() static method, returning Stream<Arguments>. | ||
* | ||
* @param property The name of the property on the model. | ||
* @param value The expected value to get (and set). | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource("provideGetterParameters") | ||
public void testGetter(String property, Object value) { | ||
ReflectionTestUtils.setField(getInstance(), property, value); | ||
|
||
assertEquals(value, ReflectionTestUtils.invokeGetterMethod(getInstance(), property), GETTER_MESSAGE); | ||
} | ||
|
||
/** | ||
* Perform the test for the given setter. | ||
* | ||
* The implementing class must implement the provideSetterParameters() static method, returning Stream<Arguments>. | ||
* | ||
* @param property The name of the property on the model. | ||
* @param value The expected value to set (and get). | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource("provideSetterParameters") | ||
public void testSetter(String property, Object value) { | ||
ReflectionTestUtils.invokeSetterMethod(getInstance(), property, value); | ||
|
||
assertEquals(value, ReflectionTestUtils.getField(getInstance(), property), SETTER_MESSAGE); | ||
} | ||
|
||
/** | ||
* Provide the instance of the given model M. | ||
* | ||
* @return The instance for the testGetter() and the testSetter() methods to use. | ||
*/ | ||
abstract protected M getInstance(); | ||
|
||
} |