Skip to content

Commit

Permalink
Add AbstractModelTest and AbstractEnumTest classes for model and enum…
Browse files Browse the repository at this point in the history
…eration testing.

This slightly simplifies creating the model and enum tests by providing otherwise redundant aspects in an abstract class.
  • Loading branch information
kaladay committed Jul 13, 2023
1 parent ae1e904 commit 442e21f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/test/java/org/tdl/vireo/model/AbstractEnumTest.java
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);
}

}
58 changes: 58 additions & 0 deletions src/test/java/org/tdl/vireo/model/AbstractModelTest.java
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();

}

0 comments on commit 442e21f

Please sign in to comment.