From df320b53a6cefef9e91eea77b4215898923b8c37 Mon Sep 17 00:00:00 2001 From: Tobias Knell Date: Tue, 20 Feb 2024 11:52:59 +0100 Subject: [PATCH] Update java version to 21, update libraries, fix some warnings --- .github/workflows/maven.yml | 6 +- pom.xml | 36 +++++----- .../java/org/synyx/beanfiller/BeanFiller.java | 4 +- .../beanfiller/creator/ArrayCreator.java | 2 +- .../synyx/beanfiller/creator/EnumCreator.java | 2 +- .../creator/SimpleArrayCreator.java | 2 +- .../beanfiller/creator/SimpleEnumCreator.java | 2 +- .../org/synyx/beanfiller/domain/History.java | 16 ++--- .../beanfiller/services/BeanAnalyzer.java | 10 +-- .../beanfiller/services/CreatorRegistry.java | 10 +-- .../strategies/AbstractCreatorStrategy.java | 8 +-- .../beanfiller/strategies/ArrayStrategy.java | 4 +- .../strategies/JustAnotherBeanStrategy.java | 16 ++--- .../strategies/SimpleObjectStrategy.java | 2 +- .../synyx/beanfiller/util/GenericsUtils.java | 2 +- .../java/org/synyx/beanfiller/ArrayTest.java | 42 ++++++------ .../java/org/synyx/beanfiller/BasicTest.java | 65 +++++++++---------- .../beanfiller/ConstructorCreationTest.java | 2 +- .../org/synyx/beanfiller/CyclingTest.java | 60 ++++++----------- .../java/org/synyx/beanfiller/EnumTest.java | 30 ++++----- .../org/synyx/beanfiller/GenericsTest.java | 39 +++++------ .../org/synyx/beanfiller/ModifierTest.java | 34 +++++----- .../SettersAndConstructorsTest.java | 60 +++++++---------- .../creator/SimpleEnumCreatorTest.java | 18 +++-- .../beanfiller/creator/StringCreatorTest.java | 21 +++--- .../beanfiller/services/AnalyzerTest.java | 32 ++++----- .../JustAnotherBeanStrategyTest.java | 13 ++-- .../strategies/StrategyManagerTest.java | 8 +-- .../beanfiller/util/RandomGeneratorTest.java | 20 +++--- 29 files changed, 247 insertions(+), 319 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 7e6643b..74373e4 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - java: [ '8' ] + java: [ '21' ] steps: - uses: actions/checkout@v2 - name: Set up JDK @@ -28,14 +28,14 @@ jobs: - name: Compile run: ./mvnw --batch-mode clean compile build: - name: build with jdk 8 + name: build with jdk 21 runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up JDK uses: actions/setup-java@v1 with: - java-version: 8 + java-version: 21 - uses: actions/cache@v2 with: path: ~/.m2/repository diff --git a/pom.xml b/pom.xml index 4bf1154..09f62bc 100644 --- a/pom.xml +++ b/pom.xml @@ -21,8 +21,8 @@ UTF-8 - 1.8 - 1.8 + 21 + 21 @@ -74,51 +74,49 @@ org.slf4j slf4j-api - 1.7.30 + 2.0.12 - org.junit.vintage - junit-vintage-engine - 5.6.2 + org.junit.jupiter + junit-jupiter + 5.10.1 + test org.mockito mockito-core - 3.5.15 + 5.10.0 test org.hamcrest - hamcrest-core + hamcrest 2.2 test - org.apache.logging.log4j - log4j-core - 2.13.3 - test - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.13.3 + org.slf4j + slf4j-simple + 2.0.12 test + + maven-surefire-plugin + 3.2.5 + org.apache.maven.plugins maven-release-plugin - 2.5.3 + 3.0.1 v@{project.version} diff --git a/src/main/java/org/synyx/beanfiller/BeanFiller.java b/src/main/java/org/synyx/beanfiller/BeanFiller.java index e73d3a7..dafe560 100644 --- a/src/main/java/org/synyx/beanfiller/BeanFiller.java +++ b/src/main/java/org/synyx/beanfiller/BeanFiller.java @@ -114,7 +114,7 @@ public T fillBean(Class clazz) throws FillingException { * @param clazz class for which the creator should be used * @param creator creator that should be used for the given class */ - public void addCreator(Class clazz, Creator creator) { + public void addCreator(Class clazz, Creator creator) { if (clazz == null || creator == null) { LOG.warn("Class or Creator is null, abort adding the Creator!"); @@ -137,7 +137,7 @@ public void addCreator(Class clazz, Creator creator) { * @param attributeName attribute for which the creator should be used * @param creator creator that should be used for the given attribute of the given class */ - public void addCreatorForClassAndAttribute(Class clazz, String attributeName, Creator creator) { + public void addCreatorForClassAndAttribute(Class clazz, String attributeName, Creator creator) { if (clazz == null || attributeName == null || creator == null) { LOG.warn("Class, attributeName, or Creator is null, abort adding the creator!"); diff --git a/src/main/java/org/synyx/beanfiller/creator/ArrayCreator.java b/src/main/java/org/synyx/beanfiller/creator/ArrayCreator.java index 7f88a49..37f7296 100644 --- a/src/main/java/org/synyx/beanfiller/creator/ArrayCreator.java +++ b/src/main/java/org/synyx/beanfiller/creator/ArrayCreator.java @@ -22,7 +22,7 @@ public interface ArrayCreator extends Creator { * * @throws FillingException if an error occurred. */ - Object createArray(List objects, Class arrayType) throws FillingException; + Object createArray(List objects, Class arrayType) throws FillingException; /** diff --git a/src/main/java/org/synyx/beanfiller/creator/EnumCreator.java b/src/main/java/org/synyx/beanfiller/creator/EnumCreator.java index 51b7092..e686980 100644 --- a/src/main/java/org/synyx/beanfiller/creator/EnumCreator.java +++ b/src/main/java/org/synyx/beanfiller/creator/EnumCreator.java @@ -19,5 +19,5 @@ public interface EnumCreator extends Creator { * * @throws FillingException if an error occurred. */ - Object createEnum(Class clazz) throws FillingException; + Object createEnum(Class clazz) throws FillingException; } diff --git a/src/main/java/org/synyx/beanfiller/creator/SimpleArrayCreator.java b/src/main/java/org/synyx/beanfiller/creator/SimpleArrayCreator.java index 401712d..30f1687 100644 --- a/src/main/java/org/synyx/beanfiller/creator/SimpleArrayCreator.java +++ b/src/main/java/org/synyx/beanfiller/creator/SimpleArrayCreator.java @@ -35,7 +35,7 @@ public SimpleArrayCreator(ArrayCriteria arrayCriteria) { } @Override - public Object createArray(List objects, Class arrayType) { + public Object createArray(List objects, Class arrayType) { // create a new instance of the array with the given type Object array = Array.newInstance(arrayType, getSize()); diff --git a/src/main/java/org/synyx/beanfiller/creator/SimpleEnumCreator.java b/src/main/java/org/synyx/beanfiller/creator/SimpleEnumCreator.java index b1be630..96f148c 100644 --- a/src/main/java/org/synyx/beanfiller/creator/SimpleEnumCreator.java +++ b/src/main/java/org/synyx/beanfiller/creator/SimpleEnumCreator.java @@ -14,7 +14,7 @@ public class SimpleEnumCreator implements EnumCreator { @Override - public Object createEnum(Class clazz) throws NoEnumConstantsException { + public Object createEnum(Class clazz) throws NoEnumConstantsException { Object[] enumArray = clazz.getEnumConstants(); diff --git a/src/main/java/org/synyx/beanfiller/domain/History.java b/src/main/java/org/synyx/beanfiller/domain/History.java index fdb9b41..4d2abee 100644 --- a/src/main/java/org/synyx/beanfiller/domain/History.java +++ b/src/main/java/org/synyx/beanfiller/domain/History.java @@ -20,7 +20,7 @@ public class History { private static final Logger LOG = LoggerFactory.getLogger(History.class); - private List filledClasses; + private List> filledClasses; private boolean isRepeating = false; /** @@ -51,13 +51,13 @@ public History(History parent) { * * @return List of previously filled classes. */ - public List getFilledClasses() { + public List> getFilledClasses() { return new ArrayList<>(filledClasses); } - public void setFilledClasses(List filledClasses) { + public void setFilledClasses(List> filledClasses) { this.filledClasses = filledClasses; } @@ -92,7 +92,7 @@ public boolean isRepeating() { */ private void update(ObjectInformation objectInformation) { - Class currentClass = objectInformation.getClazz(); + Class currentClass = objectInformation.getClazz(); Field currentField = objectInformation.getField(); checkForCycle(currentClass, currentField); @@ -106,9 +106,9 @@ private void update(ObjectInformation objectInformation) { * @param currentClass the current class to check. * @param currentField the current field to check. */ - private void checkForCycle(Class currentClass, Field currentField) { + private void checkForCycle(Class currentClass, Field currentField) { - List classesToCheck = new ArrayList<>(); + List> classesToCheck = new ArrayList<>(); classesToCheck.add(currentClass); // we also have to check for the actual type arguments for the case we have a class with generics. @@ -116,7 +116,7 @@ private void checkForCycle(Class currentClass, Field currentField) { for (Type type : actualTypeArguments) { try { - Class clazz = GenericsUtils.getClassForType(type); + Class clazz = GenericsUtils.getClassForType(type); classesToCheck.add(clazz); } catch (ClassNotFoundException ex) { @@ -126,7 +126,7 @@ private void checkForCycle(Class currentClass, Field currentField) { } // if one of the classes is contained in the history, the given objectInformation is creating a cycle. - for (Class clazz : classesToCheck) { + for (Class clazz : classesToCheck) { if (filledClasses.contains(clazz)) { isRepeating = true; diff --git a/src/main/java/org/synyx/beanfiller/services/BeanAnalyzer.java b/src/main/java/org/synyx/beanfiller/services/BeanAnalyzer.java index c09bd15..1d45343 100644 --- a/src/main/java/org/synyx/beanfiller/services/BeanAnalyzer.java +++ b/src/main/java/org/synyx/beanfiller/services/BeanAnalyzer.java @@ -32,9 +32,9 @@ public class BeanAnalyzer { * * @return List of ObjectInformation from the given class. */ - public static List analyzeBean(Class clazz) { + public static List analyzeBean(Class clazz) { - List objectInformation = new ArrayList(); + List objectInformation = new ArrayList<>(); String path = clazz.getSimpleName(); @@ -48,7 +48,7 @@ public static List analyzeBean(Class clazz) { if (setter != null) { // get the parameter type of the setter - Class parameterClazz = setter.getParameterTypes()[0]; + Class parameterClazz = setter.getParameterTypes()[0]; ObjectInformation parameterObjectInformation = new ObjectInformation(parameterClazz, field, field.getType(), setter, fieldPath, null); @@ -68,7 +68,7 @@ public static List analyzeBean(Class clazz) { * * @return Map of the setters. */ - private static Map getSetters(Class clazz) { + private static Map getSetters(Class clazz) { // get the methods of the class Method[] methods = clazz.getMethods(); @@ -78,7 +78,7 @@ private static Map getSetters(Class clazz) { for (Method method : methods) { // method name start with set, it is public and it has exactly one parameter. if (method.getName().startsWith("set") && Modifier.isPublic(method.getModifiers()) - && method.getParameterTypes() != null && method.getParameterTypes().length == 1) { + && method.getParameterTypes().length == 1) { setters.put(method.getName().toLowerCase(), method); } } diff --git a/src/main/java/org/synyx/beanfiller/services/CreatorRegistry.java b/src/main/java/org/synyx/beanfiller/services/CreatorRegistry.java index 55f8366..e695d0e 100644 --- a/src/main/java/org/synyx/beanfiller/services/CreatorRegistry.java +++ b/src/main/java/org/synyx/beanfiller/services/CreatorRegistry.java @@ -35,7 +35,7 @@ public CreatorRegistry(Map creatorMap) { * @param attributeName attribute name the creator should be used on * @param creator creator to add. */ - public void addCreatorForClassAndAttribute(Class clazz, String attributeName, Creator creator) { + public void addCreatorForClassAndAttribute(Class clazz, String attributeName, Creator creator) { classAndAttributeSpecificCreatorMap.put(clazz.getName() + "." + attributeName, creator); } @@ -47,7 +47,7 @@ public void addCreatorForClassAndAttribute(Class clazz, String attributeName, Cr * @param clazz class to use the creator on. * @param creator creator to add. */ - public void addCreator(Class clazz, Creator creator) { + public void addCreator(Class clazz, Creator creator) { creatorMap.put(clazz.getName(), creator); } @@ -61,7 +61,7 @@ public void addCreator(Class clazz, Creator creator) { * * @return a Creator or null if none was found. */ - public Creator getCreator(Class clazz, Field field) { + public Creator getCreator(Class clazz, Field field) { Creator c = null; @@ -86,7 +86,7 @@ public Creator getCreator(Class clazz, Field field) { * * @return the Creator or null, if none was found for this combination */ - private Creator getSpecificCreator(Class clazz, Field field) { + private Creator getSpecificCreator(Class clazz, Field field) { // get the creator for the class and attribute return classAndAttributeSpecificCreatorMap.get(clazz.getName() + "." + field.getName()); @@ -100,7 +100,7 @@ private Creator getSpecificCreator(Class clazz, Field field) { * * @return the Creator if found, or null. */ - public Creator getCreatorForClass(Class clazz) { + public Creator getCreatorForClass(Class clazz) { // if no specific creator for this field was set, get the creator for the class of the parameter // (class name because of primitive types) diff --git a/src/main/java/org/synyx/beanfiller/strategies/AbstractCreatorStrategy.java b/src/main/java/org/synyx/beanfiller/strategies/AbstractCreatorStrategy.java index e7fa94c..aaf0490 100644 --- a/src/main/java/org/synyx/beanfiller/strategies/AbstractCreatorStrategy.java +++ b/src/main/java/org/synyx/beanfiller/strategies/AbstractCreatorStrategy.java @@ -177,7 +177,7 @@ public int compareTo(AbstractCreatorStrategy o) { * * @return true if the given class is an enum, false otherwise. */ - protected boolean isEnum(Class clazz) { + protected boolean isEnum(Class clazz) { return clazz.isEnum(); } @@ -190,7 +190,7 @@ protected boolean isEnum(Class clazz) { * * @return true if the given class is an array, false otherwise. */ - protected boolean isArray(Class clazz) { + protected boolean isArray(Class clazz) { return clazz.isArray(); } @@ -203,7 +203,7 @@ protected boolean isArray(Class clazz) { * * @return true if the given class is a collection, false otherwise. */ - protected boolean isCollection(Class clazz) { + protected boolean isCollection(Class clazz) { return Collection.class.isAssignableFrom(clazz); } @@ -318,7 +318,7 @@ private ObjectInformation createObjectInformationForType(Type type, ObjectInform throws FillingException { try { - Class clazz = GenericsUtils.getClassForType(type); + Class clazz = GenericsUtils.getClassForType(type); return new ObjectInformation(clazz, parentObjectInformation.getField(), type, null, null, parentObjectInformation); diff --git a/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java b/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java index 5449613..3b60d0d 100644 --- a/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java +++ b/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java @@ -44,9 +44,9 @@ public Object createObjectInternal(ObjectInformation objectInformation) throws F ArrayCreator arrayCreator = (ArrayCreator) creator; - Class arrayType = objectInformation.getClazz().getComponentType(); + Class arrayType = objectInformation.getClazz().getComponentType(); - List objectsForArray = createObjectsForArray(arrayType, objectInformation.getField(), + List objectsForArray = createObjectsForArray(arrayType, objectInformation.getField(), arrayCreator.getSize(), objectInformation); return arrayCreator.createArray(objectsForArray, arrayType); diff --git a/src/main/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategy.java b/src/main/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategy.java index 72c082e..58a09b5 100644 --- a/src/main/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategy.java +++ b/src/main/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategy.java @@ -1,8 +1,5 @@ package org.synyx.beanfiller.strategies; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.synyx.beanfiller.domain.ObjectInformation; import org.synyx.beanfiller.exceptions.FillingException; import org.synyx.beanfiller.services.BeanAnalyzer; @@ -10,13 +7,8 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; - import java.lang.reflect.Type; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** @@ -27,7 +19,6 @@ */ public class JustAnotherBeanStrategy extends AbstractCreatorStrategy { - private static final Logger LOG = LoggerFactory.getLogger(JustAnotherBeanStrategy.class); /** * Creates new JustAnotherBeanStrategy. @@ -59,7 +50,7 @@ public Object createObjectInternal(ObjectInformation parentInformation) throws F } // use constructor with most parameters to potentially set the most final fields - Constructor declaredConstructor = declaredConstructors.stream() + Constructor declaredConstructor = declaredConstructors.stream() .filter(constructor -> // filter copy constructors as they will cause a stackoverflow !Arrays.asList(constructor.getParameterTypes()).contains(parentClazz) @@ -82,8 +73,9 @@ public Object createObjectInternal(ObjectInformation parentInformation) throws F if (declaredConstructor.getParameterCount() == 0) { instance = declaredConstructor.newInstance(); } else { - Class[] parameterTypes = declaredConstructor.getParameterTypes(); + Class[] parameterTypes = declaredConstructor.getParameterTypes(); Type[] genericParameterTypes = declaredConstructor.getGenericParameterTypes(); + Object[] parameters = new Object[declaredConstructor.getParameterCount()]; for (int i = 0; i < parameterTypes.length; i++) { diff --git a/src/main/java/org/synyx/beanfiller/strategies/SimpleObjectStrategy.java b/src/main/java/org/synyx/beanfiller/strategies/SimpleObjectStrategy.java index 7266638..40eb640 100644 --- a/src/main/java/org/synyx/beanfiller/strategies/SimpleObjectStrategy.java +++ b/src/main/java/org/synyx/beanfiller/strategies/SimpleObjectStrategy.java @@ -33,7 +33,7 @@ public Object createObjectInternal(ObjectInformation objectInformation) throws F Creator creator = getCreator(objectInformation); checkCreatorHasRightClass(creator, SimpleCreator.class, objectInformation); - SimpleCreator simpleCreator = (SimpleCreator) creator; + SimpleCreator simpleCreator = (SimpleCreator) creator; return simpleCreator.create(); } diff --git a/src/main/java/org/synyx/beanfiller/util/GenericsUtils.java b/src/main/java/org/synyx/beanfiller/util/GenericsUtils.java index 148de06..b8842d0 100644 --- a/src/main/java/org/synyx/beanfiller/util/GenericsUtils.java +++ b/src/main/java/org/synyx/beanfiller/util/GenericsUtils.java @@ -71,7 +71,7 @@ public static boolean hasGenerics(Type type) { * * @throws ClassNotFoundException if the class was not found. */ - public static Class getClassForType(Type type) throws ClassNotFoundException { + public static Class getClassForType(Type type) throws ClassNotFoundException { // get the class of the generic type and create it String classString = type.toString().contains("class ") ? type.toString().split(" ")[1] : type.toString(); diff --git a/src/test/java/org/synyx/beanfiller/ArrayTest.java b/src/test/java/org/synyx/beanfiller/ArrayTest.java index d5e00fb..9d5d427 100644 --- a/src/test/java/org/synyx/beanfiller/ArrayTest.java +++ b/src/test/java/org/synyx/beanfiller/ArrayTest.java @@ -1,11 +1,9 @@ package org.synyx.beanfiller; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; - import org.synyx.beanfiller.creator.ArrayCreator; import org.synyx.beanfiller.creator.SimpleArrayCreator; import org.synyx.beanfiller.creator.StringCreator; @@ -14,11 +12,11 @@ import org.synyx.beanfiller.testobjects.ArraysObject; import org.synyx.beanfiller.util.RandomGenerator; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; /** @@ -35,7 +33,7 @@ public class ArrayTest { public void testObjectIsCreated() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("ArraysObject is null!", arraysObject); + assertNotNull(arraysObject); } @@ -43,7 +41,7 @@ public void testObjectIsCreated() throws FillingException { public void testStringArrayIsFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("StringArray is null!", arraysObject.getStringArray()); + assertThat(arraysObject.getStringArray(), notNullValue()); } @@ -51,7 +49,7 @@ public void testStringArrayIsFilled() throws FillingException { public void testStringArrayElementsAreFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("StringArray Element is null!", arraysObject.getStringArray()[0]); + assertThat(arraysObject.getStringArray()[0], notNullValue()); } @@ -59,7 +57,7 @@ public void testStringArrayElementsAreFilled() throws FillingException { public void testIntArrayIsFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("IntArray is null!", arraysObject.getIntArray()); + assertThat(arraysObject.getIntArray(), notNullValue()); } @@ -67,7 +65,7 @@ public void testIntArrayIsFilled() throws FillingException { public void testIntArrayElementsAreFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotEquals("IntArray Element is zero!", 0, arraysObject.getIntArray()[0]); + assertThat(arraysObject.getIntArray()[0], is(not(0))); } @@ -75,7 +73,7 @@ public void testIntArrayElementsAreFilled() throws FillingException { public void testObjectArrayIsFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("ObjectArray is null!", arraysObject.getObjectArray()); + assertThat(arraysObject.getObjectArray(), notNullValue()); } @@ -83,7 +81,7 @@ public void testObjectArrayIsFilled() throws FillingException { public void testObjectArrayElementsAreFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("ObjectArray Element is null!", arraysObject.getObjectArray()[0]); + assertThat(arraysObject.getObjectArray()[0], notNullValue()); } @@ -91,7 +89,7 @@ public void testObjectArrayElementsAreFilled() throws FillingException { public void testEnumArrayIsFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("EnumArray is null!", arraysObject.getEnumArray()); + assertThat(arraysObject.getEnumArray(), notNullValue()); } @@ -99,7 +97,7 @@ public void testEnumArrayIsFilled() throws FillingException { public void testEnumArrayElementsAreFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("EnumArray Element is null!", arraysObject.getEnumArray()[0]); + assertThat(arraysObject.getEnumArray()[0], notNullValue()); } @@ -107,7 +105,7 @@ public void testEnumArrayElementsAreFilled() throws FillingException { public void testListArrayIsFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("ListArray is null!", arraysObject.getListArray()); + assertThat(arraysObject.getListArray(), notNullValue()); } @@ -115,7 +113,7 @@ public void testListArrayIsFilled() throws FillingException { public void testListArrayElementsAreFilled() throws FillingException { arraysObject = beanfiller.fillBean(ArraysObject.class); - assertNotNull("ListArray Element is null!", arraysObject.getListArray()[0]); + assertThat(arraysObject.getListArray()[0], notNullValue()); } @@ -149,13 +147,13 @@ public void testAddedSpecificArrayCreatorIsUsed() throws FillingException { } - @Test(expected = WrongCreatorException.class) - public void testWrongCreatorExceptionIsThrownIfNonArrayCreatorIsUsed() throws FillingException { + @Test + public void testWrongCreatorExceptionIsThrownIfNonArrayCreatorIsUsed() { StringCreator stringCreator = new StringCreator(new RandomGenerator()); beanfiller.addCreatorForClassAndAttribute(ArraysObject.class, "stringArray", stringCreator); - beanfiller.fillBean(ArraysObject.class); + assertThrows(WrongCreatorException.class, () -> beanfiller.fillBean(ArraysObject.class)); } } diff --git a/src/test/java/org/synyx/beanfiller/BasicTest.java b/src/test/java/org/synyx/beanfiller/BasicTest.java index b848d6a..d3333f5 100644 --- a/src/test/java/org/synyx/beanfiller/BasicTest.java +++ b/src/test/java/org/synyx/beanfiller/BasicTest.java @@ -1,9 +1,7 @@ package org.synyx.beanfiller; -import org.junit.Assert; -import org.junit.Test; - +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.creator.EnumCreator; import org.synyx.beanfiller.creator.SimpleEnumCreator; import org.synyx.beanfiller.creator.StringCreator; @@ -11,11 +9,10 @@ import org.synyx.beanfiller.exceptions.WrongCreatorException; import org.synyx.beanfiller.testobjects.BaseObject; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; /** @@ -32,7 +29,7 @@ public class BasicTest { public void testObjectIsCreated() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("BaseObject is null!", baseObject); + assertThat(baseObject, notNullValue()); } @@ -40,7 +37,7 @@ public void testObjectIsCreated() throws FillingException { public void testStringIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("String is not being filled!", baseObject.getText()); + assertThat(baseObject.getText(), notNullValue()); } @@ -48,7 +45,7 @@ public void testStringIsFilled() throws FillingException { public void testIntegerIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Integer is not being filled!", baseObject.getIntegerNumber()); + assertThat(baseObject.getIntegerNumber(), notNullValue()); } @@ -56,7 +53,7 @@ public void testIntegerIsFilled() throws FillingException { public void testPrimitiveIntegerIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("primitive Integer (int) is not being filled!", baseObject.getPrimitiveIntNumber()); + assertThat(baseObject.getPrimitiveIntNumber(), greaterThan(0)); } @@ -64,7 +61,7 @@ public void testPrimitiveIntegerIsFilled() throws FillingException { public void testFloatIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Float is not being filled!", baseObject.getFloatNumber()); + assertThat(baseObject.getFloatNumber(), notNullValue()); } @@ -72,7 +69,7 @@ public void testFloatIsFilled() throws FillingException { public void testPrimitiveFloatIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("primitive Float (float) is not being filled!", baseObject.getPrimitiveFloatNumber()); + assertThat(baseObject.getPrimitiveFloatNumber(), greaterThan(0F)); } @@ -80,7 +77,7 @@ public void testPrimitiveFloatIsFilled() throws FillingException { public void testLongIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Long is not being filled!", baseObject.getLongNumber()); + assertThat(baseObject.getLongNumber(), notNullValue()); } @@ -88,7 +85,7 @@ public void testLongIsFilled() throws FillingException { public void testPrimitiveLongIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("primitive Long (long) is not being filled!", baseObject.getPrimitiveLongNumber()); + assertThat(baseObject.getPrimitiveLongNumber(), greaterThan(0L)); } @@ -96,7 +93,7 @@ public void testPrimitiveLongIsFilled() throws FillingException { public void testDoubleIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Double is not being filled!", baseObject.getDoubleNumber()); + assertThat(baseObject.getDoubleNumber(), notNullValue()); } @@ -104,7 +101,7 @@ public void testDoubleIsFilled() throws FillingException { public void testPrimitiveDoubleIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("primitive Double (double) is not being filled!", baseObject.getPrimitiveDouble()); + assertThat(baseObject.getPrimitiveDouble(), greaterThan(0D)); } @@ -112,7 +109,7 @@ public void testPrimitiveDoubleIsFilled() throws FillingException { public void testBooleanIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Boolean is not being filled!", baseObject.getBooleanObject()); + assertThat(baseObject.getBooleanObject(), notNullValue()); } @@ -120,7 +117,7 @@ public void testBooleanIsFilled() throws FillingException { public void testPrimitiveBooleanIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("primitive Boolean (boolean) is not being filled!", baseObject.getPrimitiveBoolean()); + assertThat(baseObject.getPrimitiveBoolean(), notNullValue()); } @@ -128,7 +125,7 @@ public void testPrimitiveBooleanIsFilled() throws FillingException { public void testByteIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Byte is not being filled!", baseObject.getByteObject()); + assertThat(baseObject.getByteObject(), notNullValue()); } @@ -136,8 +133,7 @@ public void testByteIsFilled() throws FillingException { public void testPrimitiveByteIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - - assertNotNull("primitive Byte (byte) is not being filled!", baseObject.getPrimitiveByte()); + assertThat(baseObject.getPrimitiveByte(), is(not(0))); } @@ -145,7 +141,7 @@ public void testPrimitiveByteIsFilled() throws FillingException { public void testBigIntegerIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("BigInteger is not being filled!", baseObject.getBigIntegerNumber()); + assertThat(baseObject.getBigIntegerNumber(), notNullValue()); } @@ -153,7 +149,7 @@ public void testBigIntegerIsFilled() throws FillingException { public void testBigDecimalIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("BigDecimal is not being filled!", baseObject.getBigDecimalNumber()); + assertThat(baseObject.getBigDecimalNumber(), notNullValue()); } @@ -161,7 +157,7 @@ public void testBigDecimalIsFilled() throws FillingException { public void testDateIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Date is not being filled!", baseObject.getDate()); + assertThat(baseObject.getDate(), notNullValue()); } @@ -169,7 +165,7 @@ public void testDateIsFilled() throws FillingException { public void testObjectShortIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("ObjectShort is not being filled!", baseObject.getObjectShort()); + assertThat(baseObject.getObjectShort(), notNullValue()); } @@ -177,7 +173,7 @@ public void testObjectShortIsFilled() throws FillingException { public void testPrimitiveShortIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("PrimitiveShort is not being filled!", baseObject.getPrimitiveShort()); + assertThat(baseObject.getPrimitiveShort(), greaterThan((short)0)); } @@ -185,7 +181,7 @@ public void testPrimitiveShortIsFilled() throws FillingException { public void testCharacterIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("Character is not being filled!", baseObject.getCharacter()); + assertThat(baseObject.getCharacter(), notNullValue()); } @@ -193,7 +189,7 @@ public void testCharacterIsFilled() throws FillingException { public void testPrimitiveCharacterIsFilled() throws FillingException { baseObject = beanfiller.fillBean(BaseObject.class); - assertNotNull("char is not being filled!", baseObject.getPrimitiveCharacter()); + assertThat(baseObject.getPrimitiveCharacter(), greaterThan((char)0)); } @@ -227,14 +223,13 @@ public void testAddedSpecificCreatorIsUsed() throws FillingException { } - @Test(expected = WrongCreatorException.class) - public void testWrongCreatorExceptionIsThrownIfNonSimpleCreatorIsUsed() throws FillingException { + @Test + public void testWrongCreatorExceptionIsThrownIfNonSimpleCreatorIsUsed() { EnumCreator enumCreator = new SimpleEnumCreator(); - beanfiller.addCreatorForClassAndAttribute(BaseObject.class, "text", enumCreator); - beanfiller.fillBean(BaseObject.class); + assertThrows(WrongCreatorException.class, () -> beanfiller.fillBean(BaseObject.class)); } @@ -242,6 +237,6 @@ public void testWrongCreatorExceptionIsThrownIfNonSimpleCreatorIsUsed() throws F public void testSetterObjectDiffersFromFieldObject() throws FillingException { BaseObject object = beanfiller.fillBean(BaseObject.class); - assertNotEquals(0, object.getDateFromTimestamp()); + assertThat(object.getDateFromTimestamp(), greaterThan(0L)); } } diff --git a/src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java b/src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java index d4eb3b6..051d0e8 100644 --- a/src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java +++ b/src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java @@ -1,6 +1,6 @@ package org.synyx.beanfiller; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.exceptions.FillingException; import org.synyx.beanfiller.testobjects.ArrayConstructorObject; import org.synyx.beanfiller.testobjects.ListConstructorObject; diff --git a/src/test/java/org/synyx/beanfiller/CyclingTest.java b/src/test/java/org/synyx/beanfiller/CyclingTest.java index 3047db2..6322349 100644 --- a/src/test/java/org/synyx/beanfiller/CyclingTest.java +++ b/src/test/java/org/synyx/beanfiller/CyclingTest.java @@ -1,20 +1,12 @@ package org.synyx.beanfiller; -import org.junit.Assert; -import org.junit.Test; - +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.exceptions.FillingException; -import org.synyx.beanfiller.testobjects.cycling.ArrayCycleObject; -import org.synyx.beanfiller.testobjects.cycling.ArrayCycleObject2; -import org.synyx.beanfiller.testobjects.cycling.ArrayCycleObject3; -import org.synyx.beanfiller.testobjects.cycling.ArrayCycleObject4; -import org.synyx.beanfiller.testobjects.cycling.CyclicObject; -import org.synyx.beanfiller.testobjects.cycling.CyclicObject2; -import org.synyx.beanfiller.testobjects.cycling.ListCycleObject; -import org.synyx.beanfiller.testobjects.cycling.MapCycleObject; +import org.synyx.beanfiller.testobjects.cycling.*; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; /** @@ -31,7 +23,7 @@ public void testBeanCycle() throws FillingException { CyclicObject object = beanfiller.fillBean(CyclicObject.class); - assertNull("The cycling Object should not be filled!", object.getCycle()); + assertThat(object.getCycle(), nullValue()); } @@ -40,12 +32,10 @@ public void testDeeperBeanCycle() throws FillingException { CyclicObject2 object = beanfiller.fillBean(CyclicObject2.class); - assertNotNull("No cycle yet, this should be filled!", object.getCyclicObject3()); - - assertNotNull("No cycle yet, this should be filled!", object.getCyclicObject3().getCyclicObject4()); + assertThat(object.getCyclicObject3(), notNullValue()); + assertThat(object.getCyclicObject3().getCyclicObject4(), notNullValue()); - assertNull("The cycling Object should not be filled!", - object.getCyclicObject3().getCyclicObject4().getCyclicObject2()); + assertThat(object.getCyclicObject3().getCyclicObject4().getCyclicObject2(), nullValue()); } @@ -55,16 +45,14 @@ public void testCyclesWithArrays() throws FillingException { ArrayCycleObject arrayCycleObject = beanfiller.fillBean(ArrayCycleObject.class); CyclicObject2[] cyclicObject2Array = arrayCycleObject.getCyclicObject2array(); - assertNotNull("No cycle yet, this should be filled!", cyclicObject2Array); + assertThat(cyclicObject2Array, notNullValue()); for (CyclicObject2 object : cyclicObject2Array) { - assertNotNull("No cycle yet, this should be filled!", object); - assertNotNull("No cycle yet, this should be filled!", object.getCyclicObject3()); - - assertNotNull("No cycle yet, this should be filled!", object.getCyclicObject3().getCyclicObject4()); + assertThat(object, notNullValue()); + assertThat(object.getCyclicObject3(), notNullValue()); + assertThat(object.getCyclicObject3().getCyclicObject4(), notNullValue()); - assertNull("The cycling Object should not be filled!", - object.getCyclicObject3().getCyclicObject4().getCyclicObject2()); + assertThat(object.getCyclicObject3().getCyclicObject4().getCyclicObject2(), nullValue()); } } @@ -75,18 +63,15 @@ public void testCyclesWithOnlyArrays() throws FillingException { ArrayCycleObject4 arrayCycleObject4 = beanfiller.fillBean(ArrayCycleObject4.class); ArrayCycleObject2[] array = arrayCycleObject4.getArrayCycleObject2Array(); - assertNotNull("No cycle yet, the array on arrayCycleObject4 should be filled!", array); + assertThat(array, notNullValue()); for (ArrayCycleObject2 arrayCycleObject2 : array) { - assertNotNull("No cycle yet, arrayCycleObject2 should be filled!", arrayCycleObject2); - - assertNotNull("No cycle yet, the array on arrayCycleObject2 should be filled!", - arrayCycleObject2.getArrayCycleObject3Array()); + assertThat(arrayCycleObject2, notNullValue()); + assertThat(arrayCycleObject2.getArrayCycleObject3Array(), notNullValue()); for (ArrayCycleObject3 arrayCycleObject3 : arrayCycleObject2.getArrayCycleObject3Array()) { - assertNotNull("No cycle yet, arrayCycleObject3 should be filled!", arrayCycleObject3); - assertNull("The Array on arrayCycleObject3 should not be filled as it is introducing a cycle!", - arrayCycleObject3.getArrayCycleObject3Array()); + assertThat(arrayCycleObject3, notNullValue()); + assertThat(arrayCycleObject3.getArrayCycleObject3Array(), nullValue()); } } } @@ -96,8 +81,7 @@ public void testCyclesWithOnlyArrays() throws FillingException { public void testCyclesWithMap() throws FillingException { MapCycleObject mapCycleObject = beanfiller.fillBean(MapCycleObject.class); - - assertNull("The map should not be filled, because it is introducing a cycle!", mapCycleObject.getMap()); + assertThat(mapCycleObject.getMap(), nullValue()); } @@ -105,8 +89,6 @@ public void testCyclesWithMap() throws FillingException { public void testCyclesWithList() throws FillingException { ListCycleObject listCycleObject = beanfiller.fillBean(ListCycleObject.class); - - assertNull("The list should not be filled, because it is introducing a cycle!", - listCycleObject.getList()); + assertThat(listCycleObject.getList(), nullValue()); } } diff --git a/src/test/java/org/synyx/beanfiller/EnumTest.java b/src/test/java/org/synyx/beanfiller/EnumTest.java index be99b27..ebf1ac2 100644 --- a/src/test/java/org/synyx/beanfiller/EnumTest.java +++ b/src/test/java/org/synyx/beanfiller/EnumTest.java @@ -1,9 +1,7 @@ package org.synyx.beanfiller; -import org.junit.Assert; -import org.junit.Test; - +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.creator.EnumCreator; import org.synyx.beanfiller.creator.SimpleCreator; import org.synyx.beanfiller.creator.SimpleEnumCreator; @@ -15,10 +13,10 @@ import org.synyx.beanfiller.testobjects.TestEnum; import org.synyx.beanfiller.util.RandomGenerator; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; /** @@ -34,15 +32,14 @@ public class EnumTest { public void testEnumIsFilled() throws FillingException { EnumsObject o = beanfiller.fillBean(EnumsObject.class); - - assertNotNull("TestEnum was not filled!", o.getTestEnum()); + assertThat(o.getTestEnum(), notNullValue()); } - @Test(expected = NoEnumConstantsException.class) - public void testNoEnumConstantsExceptionIsThrownOnEmptyEnum() throws FillingException { + @Test + public void testNoEnumConstantsExceptionIsThrownOnEmptyEnum() { - beanfiller.fillBean(ErrorEnumObject.class); + assertThrows(NoEnumConstantsException.class, () -> beanfiller.fillBean(ErrorEnumObject.class)); } @@ -75,13 +72,12 @@ public void testAddedSpecificEnumCreatorIsUsed() throws FillingException { } - @Test(expected = WrongCreatorException.class) - public void testWrongCreatorExceptionIsThrownIfNonEnumCreatorIsUsed() throws FillingException { - - SimpleCreator stringCreator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + @Test + public void testWrongCreatorExceptionIsThrownIfNonEnumCreatorIsUsed() { + SimpleCreator stringCreator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreatorForClassAndAttribute(EnumsObject.class, "testEnum", stringCreator); - beanfiller.fillBean(EnumsObject.class); + assertThrows(WrongCreatorException.class, () -> beanfiller.fillBean(EnumsObject.class)); } } diff --git a/src/test/java/org/synyx/beanfiller/GenericsTest.java b/src/test/java/org/synyx/beanfiller/GenericsTest.java index 5523369..1d8fb44 100644 --- a/src/test/java/org/synyx/beanfiller/GenericsTest.java +++ b/src/test/java/org/synyx/beanfiller/GenericsTest.java @@ -1,8 +1,6 @@ package org.synyx.beanfiller; -import org.junit.Assert; -import org.junit.Test; - +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.creator.ListCreator; import org.synyx.beanfiller.creator.SimpleCreator; import org.synyx.beanfiller.exceptions.FillingException; @@ -14,13 +12,12 @@ import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Matchers.anyList; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.Mockito.*; /** @@ -37,7 +34,7 @@ public class GenericsTest { public void testGenericsObjectIsCreated() throws FillingException { genericsObject = beanfiller.fillBean(GenericsObject.class); - assertNotNull("GenericsObject is null!", genericsObject); + assertThat(genericsObject, notNullValue()); } @@ -45,8 +42,8 @@ public void testGenericsObjectIsCreated() throws FillingException { public void testListIsFilled() throws FillingException { genericsObject = beanfiller.fillBean(GenericsObject.class); - assertNotNull("StringList is null!", genericsObject.getStringList()); - assertFalse("StringList is empty!", genericsObject.getStringList().isEmpty()); + assertThat(genericsObject.getStringList(), notNullValue()); + assertThat(genericsObject.getStringList().isEmpty(), is(false)); } @@ -54,8 +51,8 @@ public void testListIsFilled() throws FillingException { public void testMapIsFilled() throws FillingException { genericsObject = beanfiller.fillBean(GenericsObject.class); - assertNotNull("StringMap is null!", genericsObject.getStringMap()); - assertFalse("StringMap is empty!", genericsObject.getStringMap().isEmpty()); + assertThat(genericsObject.getStringMap(), notNullValue()); + assertThat(genericsObject.getStringMap().isEmpty(), is(false)); } @@ -63,8 +60,8 @@ public void testMapIsFilled() throws FillingException { public void testListMapIsFilled() throws FillingException { genericsObject = beanfiller.fillBean(GenericsObject.class); - assertNotNull("StringListMap is null!", genericsObject.getStringListMap()); - assertFalse("StringListMap is empty!", genericsObject.getStringListMap().isEmpty()); + assertThat(genericsObject.getStringListMap(), notNullValue()); + assertThat(genericsObject.getStringListMap().isEmpty(), is(false)); } @@ -98,13 +95,13 @@ public void testAddedSpecificGenericsCreatorIsUsed() throws FillingException { } - @Test(expected = WrongCreatorException.class) - public void testWrongCreatorExceptionIsThrownIfNonGenericsCreatorIsUsed() throws FillingException { + @Test + public void testWrongCreatorExceptionIsThrownIfNonGenericsCreatorIsUsed() { - SimpleCreator stringCreator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + SimpleCreator stringCreator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreatorForClassAndAttribute(GenericsObject.class, "stringList", stringCreator); - beanfiller.fillBean(GenericsObject.class); + assertThrows(WrongCreatorException.class, () -> beanfiller.fillBean(GenericsObject.class)); } } diff --git a/src/test/java/org/synyx/beanfiller/ModifierTest.java b/src/test/java/org/synyx/beanfiller/ModifierTest.java index b5566fc..7907251 100644 --- a/src/test/java/org/synyx/beanfiller/ModifierTest.java +++ b/src/test/java/org/synyx/beanfiller/ModifierTest.java @@ -1,15 +1,14 @@ package org.synyx.beanfiller; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.exceptions.FillingException; import org.synyx.beanfiller.testobjects.ModifierObject; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; /** @@ -19,56 +18,57 @@ */ public class ModifierTest { - private final BeanFiller beanfiller = new BeanFiller(); + private BeanFiller beanfiller; private ModifierObject modifierObject; - @Before + @BeforeEach public void setup() throws FillingException { + beanfiller = new BeanFiller(); modifierObject = beanfiller.fillBean(ModifierObject.class); } /** - * Test that using public setters on domain objects works. + * Test that using public static setters on domain objects works. */ @Test public void testPublicStaticSetter() { - assertNotNull(ModifierObject.getPUBLIC_STATIC_STRING()); + assertThat(ModifierObject.getPUBLIC_STATIC_STRING(), notNullValue()); } /** - * Test that using package protected setters on domain objects doesn't work. + * Test that using package protected static setters on domain objects doesn't work. */ @Test public void testPackageStaticSetter() { // we can't do that, so it's still null - assertNull(ModifierObject.getPACKAGE_STATIC_STRING()); + assertThat(ModifierObject.getPACKAGE_STATIC_STRING(), nullValue()); } /** - * Test that using protected setters on domain objects doesn't work. + * Test that using protected static setters on domain objects doesn't work. */ @Test public void testProtectedStaticSetter() { // we can't do that, so it's still null - assertNull(ModifierObject.getPROTECTED_STATIC_STRING()); + assertThat(ModifierObject.getPROTECTED_STATIC_STRING(), nullValue()); } /** - * Test that using private setters on domain objects doesn't work. + * Test that using private static setters on domain objects doesn't work. */ @Test public void testPrivateStaticSetter() { // we can't do that, so it's still null - assertNull(ModifierObject.getPRIVATE_STATIC_STRING()); + assertThat(ModifierObject.getPRIVATE_STATIC_STRING(), nullValue()); } @@ -79,6 +79,6 @@ public void testPrivateStaticSetter() { public void testPublicFinalString() { // has to be null, because it's final and initialized with null - assertNull(modifierObject.getPublicFinalString()); + assertThat(modifierObject.getPublicFinalString(), nullValue()); } } diff --git a/src/test/java/org/synyx/beanfiller/SettersAndConstructorsTest.java b/src/test/java/org/synyx/beanfiller/SettersAndConstructorsTest.java index 9c248d3..7f57d4a 100644 --- a/src/test/java/org/synyx/beanfiller/SettersAndConstructorsTest.java +++ b/src/test/java/org/synyx/beanfiller/SettersAndConstructorsTest.java @@ -1,7 +1,7 @@ package org.synyx.beanfiller; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.creator.Creator; import org.synyx.beanfiller.creator.SimpleCreator; import org.synyx.beanfiller.util.RandomGenerator; @@ -9,7 +9,9 @@ import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.core.Is.is; /** @@ -23,10 +25,9 @@ public class SettersAndConstructorsTest { public void testDefaultCreatorMapIsUsed() { BeanFiller beanfiller = new BeanFiller(); - assertNotNull("The CreatorMap shouldn't be null if the default Creator map is used!", - beanfiller.getCreatorMap()); - assertFalse("The CreatorMap shouldn't be empty if the default Creator map is used!", - beanfiller.getCreatorMap().isEmpty()); + + assertThat(beanfiller.getCreatorMap(), notNullValue()); + assertThat(beanfiller.getCreatorMap().isEmpty(), is(false)); } @@ -34,8 +35,8 @@ public void testDefaultCreatorMapIsUsed() { public void testSetCreatorMapWithConstructor() { BeanFiller beanfiller = new BeanFiller(new HashMap<>()); - assertNotNull("The CreatorMap shouldn't be null", beanfiller.getCreatorMap()); - assertTrue("The CreatorMap should be empty as we set it above!", beanfiller.getCreatorMap().isEmpty()); + assertThat(beanfiller.getCreatorMap(), notNullValue()); + assertThat(beanfiller.getCreatorMap().isEmpty(), is(true)); } @@ -47,13 +48,11 @@ public void testAddCreator() { BeanFiller beanfiller = new BeanFiller(); - SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreator(this.getClass(), creator); Map creatorMap = beanfiller.getCreatorMap(); - - assertEquals("Creator wasn't added under the expected key!", creator, - creatorMap.get(this.getClass().getName())); + assertThat(creatorMap.get(this.getClass().getName()), is(creator)); } @@ -65,13 +64,11 @@ public void testAddArrayCreator() { BeanFiller beanfiller = new BeanFiller(); - SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreator(String[].class, creator); Map creatorMap = beanfiller.getCreatorMap(); - - assertEquals("Creator wasn't added under the expected key!", creator, - creatorMap.get("[Ljava.lang.String;")); + assertThat(creatorMap.get("[Ljava.lang.String;"), is(creator)); } @@ -81,15 +78,13 @@ public void testAddArrayCreator() { @Test public void testAddCreatorWithoutClass() { - // we need an empty Creator map for this test BeanFiller beanfiller = new BeanFiller(new HashMap<>()); - SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreator(null, creator); Map creatorMap = beanfiller.getCreatorMap(); - - assertEquals("Creator map should be empty!", 0, creatorMap.size()); + assertThat(creatorMap.isEmpty(), is(true)); } @@ -99,14 +94,12 @@ public void testAddCreatorWithoutClass() { @Test public void testAddCreatorWithoutCreator() { - // we need an empty Creator map for this test BeanFiller beanfiller = new BeanFiller(new HashMap<>()); beanfiller.addCreator(this.getClass(), null); Map creatorMap = beanfiller.getCreatorMap(); - - assertEquals("Creator map should be empty!", 0, creatorMap.size()); + assertThat(creatorMap.isEmpty(), is(true)); } @@ -118,13 +111,11 @@ public void testAddSpecificCreator() { BeanFiller beanfiller = new BeanFiller(); - SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreatorForClassAndAttribute(this.getClass(), "test", creator); Map creatorMap = beanfiller.getClassAndAttributeSpecificCreatorMap(); - - assertEquals("Creator wasn't added under the expected key!", creator, - creatorMap.get(this.getClass().getName() + ".test")); + assertThat( creatorMap.get(this.getClass().getName() + ".test"), is(creator)); } @@ -134,15 +125,13 @@ public void testAddSpecificCreator() { @Test public void testAddSpecificCreatorWithoutClass() { - // we need an empty Creator map for this test BeanFiller beanfiller = new BeanFiller(new HashMap<>()); - SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreatorForClassAndAttribute(null, "test", creator); Map creatorMap = beanfiller.getClassAndAttributeSpecificCreatorMap(); - - assertEquals("Creator map should be empty!", 0, creatorMap.size()); + assertThat(creatorMap.isEmpty(), is(true)); } @@ -155,12 +144,11 @@ public void testAddSpecificCreatorWithoutAttributeName() { // we need an empty Creator map for this test BeanFiller beanfiller = new BeanFiller(new HashMap<>()); - SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); + SimpleCreator creator = new org.synyx.beanfiller.creator.StringCreator(new RandomGenerator()); beanfiller.addCreatorForClassAndAttribute(this.getClass(), null, creator); Map creatorMap = beanfiller.getClassAndAttributeSpecificCreatorMap(); - - assertEquals("Creator map should be empty!", 0, creatorMap.size()); + assertThat(creatorMap.isEmpty(), is(true)); } @@ -170,13 +158,11 @@ public void testAddSpecificCreatorWithoutAttributeName() { @Test public void testAddSpecificCreatorWithoutCreator() { - // we need an empty Creator map for this test BeanFiller beanfiller = new BeanFiller(new HashMap<>()); beanfiller.addCreatorForClassAndAttribute(this.getClass(), "test", null); Map creatorMap = beanfiller.getClassAndAttributeSpecificCreatorMap(); - - assertEquals("Creator map should be empty!", 0, creatorMap.size()); + assertThat(creatorMap.isEmpty(), is(true)); } } diff --git a/src/test/java/org/synyx/beanfiller/creator/SimpleEnumCreatorTest.java b/src/test/java/org/synyx/beanfiller/creator/SimpleEnumCreatorTest.java index 8aac892..122fe00 100644 --- a/src/test/java/org/synyx/beanfiller/creator/SimpleEnumCreatorTest.java +++ b/src/test/java/org/synyx/beanfiller/creator/SimpleEnumCreatorTest.java @@ -1,14 +1,15 @@ package org.synyx.beanfiller.creator; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.exceptions.FillingException; import org.synyx.beanfiller.exceptions.NoEnumConstantsException; import org.synyx.beanfiller.testobjects.EmptyEnum; import org.synyx.beanfiller.testobjects.TestEnum; -import static org.junit.Assert.assertNotNull; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.jupiter.api.Assertions.assertThrows; /** @@ -24,17 +25,14 @@ public void testEnumCreatorSelectsEnum() throws FillingException { EnumCreator enumCreator = new SimpleEnumCreator(); TestEnum testEnum = (TestEnum) enumCreator.createEnum(TestEnum.class); - // assert that a TestEnum was created - assertNotNull("TestEnum is null!", testEnum); + assertThat(testEnum, notNullValue()); } - @Test(expected = NoEnumConstantsException.class) - public void testEnumCreatorThrowsExceptionIfNoEnumConstants() throws FillingException { + @Test + public void testEnumCreatorThrowsExceptionIfNoEnumConstants() { EnumCreator enumCreator = new SimpleEnumCreator(); - - // the EmptyEnum does not have Enum Constants defined, so an Exception should be thrown. - enumCreator.createEnum(EmptyEnum.class); + assertThrows(NoEnumConstantsException.class, () -> enumCreator.createEnum(EmptyEnum.class)); } } diff --git a/src/test/java/org/synyx/beanfiller/creator/StringCreatorTest.java b/src/test/java/org/synyx/beanfiller/creator/StringCreatorTest.java index 8f475a8..8febee3 100644 --- a/src/test/java/org/synyx/beanfiller/creator/StringCreatorTest.java +++ b/src/test/java/org/synyx/beanfiller/creator/StringCreatorTest.java @@ -1,13 +1,12 @@ package org.synyx.beanfiller.creator; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.criteria.StringCriteria; import org.synyx.beanfiller.util.RandomGenerator; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -21,7 +20,7 @@ public class StringCreatorTest { private RandomGenerator randomGeneratorMock; - @Before + @BeforeEach public void setUp() { randomGeneratorMock = mock(RandomGenerator.class); @@ -40,7 +39,7 @@ public void testStringCreatorUsesCorrectMinimumLength() { StringCreator stringCreator = new StringCreator(randomGeneratorMock, new StringCriteria(min, max, "A")); String string = stringCreator.create(); - assertEquals("StringCreator has not used the given minimum length!", min, string.length()); + assertThat(string.length(), is(min)); } @@ -56,7 +55,7 @@ public void testStringCreatorUsesCorrectMaximumLength() { StringCreator stringCreator = new StringCreator(randomGeneratorMock, new StringCriteria(min, max, "A")); String string = stringCreator.create(); - assertEquals("StringCreator has not used the given maximum length!", max, string.length()); + assertThat(string.length(), is(max)); } @@ -74,8 +73,7 @@ public void testStringCreatorUsesFirstLetterOfCharset() { String string = stringCreator.create(); for (int i = 0; i < string.length(); i++) { - assertEquals("StringCreator did not only use the first letter of the charset.", 'A', - string.charAt(i)); + assertThat(string.charAt(i), is("A")); } } @@ -94,8 +92,7 @@ public void testStringCreatorUsesLastLetterOfCharset() { String string = stringCreator.create(); for (int i = 0; i < string.length(); i++) { - assertEquals("StringCreator did not only use the last letter of the charset.", 'G', - string.charAt(i)); + assertThat(string.charAt(i), is("G")); } } } diff --git a/src/test/java/org/synyx/beanfiller/services/AnalyzerTest.java b/src/test/java/org/synyx/beanfiller/services/AnalyzerTest.java index 19eeec7..0f5e34e 100644 --- a/src/test/java/org/synyx/beanfiller/services/AnalyzerTest.java +++ b/src/test/java/org/synyx/beanfiller/services/AnalyzerTest.java @@ -1,8 +1,6 @@ package org.synyx.beanfiller.services; -import org.junit.Assert; -import org.junit.Test; - +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.domain.ObjectInformation; import org.synyx.beanfiller.testobjects.BaseObject; import org.synyx.beanfiller.testobjects.ObjectWithBeans; @@ -10,7 +8,8 @@ import java.util.List; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; /** @@ -22,8 +21,7 @@ public class AnalyzerTest { public void testAnalyzerReturnsListWithRightSize() { List objectInformationList = BeanAnalyzer.analyzeBean(ObjectWithBeans.class); - - assertEquals("Expected list to have 2 entries, one for each field!", 2, objectInformationList.size()); + assertThat( objectInformationList.size(), is(2)); } @@ -35,8 +33,8 @@ public void testAnalyzerSetsClassesCorrectly() { ObjectInformation information1 = objectInformationList.get(0); ObjectInformation information2 = objectInformationList.get(1); - assertEquals("Expected first entry to be BaseObject", BaseObject.class, information1.getClazz()); - assertEquals("Expected second entry to be TestEnum", TestEnum.class, information2.getClazz()); + assertThat(information1.getClazz(), is(BaseObject.class)); + assertThat(information2.getClazz(), is(TestEnum.class)); } @@ -48,10 +46,8 @@ public void testAnalyzerSetsFieldsCorrectly() { ObjectInformation information1 = objectInformationList.get(0); ObjectInformation information2 = objectInformationList.get(1); - assertEquals("Expected first entry field to be baseObject", "baseObject", - information1.getField().getName()); - assertEquals("Expected second entry field to be testEnum", "testEnum", - information2.getField().getName()); + assertThat(information1.getField().getName(), is( "baseObject")); + assertThat(information2.getField().getName(), is( "testEnum")); } @@ -63,10 +59,8 @@ public void testAnalyzerSetsTypeCorrectly() { ObjectInformation information1 = objectInformationList.get(0); ObjectInformation information2 = objectInformationList.get(1); - assertEquals("Expected first entry type to be 'class org.synyx.beanfiller.testobjects.BaseObject'", - "class org.synyx.beanfiller.testobjects.BaseObject", information1.getType().toString()); - assertEquals("Expected second entry type to be 'class org.synyx.beanfiller.testobjects.TestEnum'", - "class org.synyx.beanfiller.testobjects.TestEnum", information2.getType().toString()); + assertThat(information1.getType().toString(), is( "class org.synyx.beanfiller.testobjects.BaseObject")); + assertThat(information2.getType().toString(), is( "class org.synyx.beanfiller.testobjects.TestEnum")); } @@ -78,9 +72,7 @@ public void testAnalyzerSetsPathCorrectly() { ObjectInformation information1 = objectInformationList.get(0); ObjectInformation information2 = objectInformationList.get(1); - assertEquals("Expected first entry path to be ObjectWithBeans.baseObject", "ObjectWithBeans.baseObject", - information1.getPath()); - assertEquals("Expected second entry path to be ObjectWithBeans.testEnum", "ObjectWithBeans.testEnum", - information2.getPath()); + assertThat(information1.getPath(), is( "ObjectWithBeans.baseObject")); + assertThat(information2.getPath(), is( "ObjectWithBeans.testEnum")); } } diff --git a/src/test/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategyTest.java b/src/test/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategyTest.java index 467eada..1bd6833 100644 --- a/src/test/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategyTest.java +++ b/src/test/java/org/synyx/beanfiller/strategies/JustAnotherBeanStrategyTest.java @@ -1,6 +1,6 @@ package org.synyx.beanfiller.strategies; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.synyx.beanfiller.BeanFiller; import org.synyx.beanfiller.domain.ObjectInformation; import org.synyx.beanfiller.exceptions.FillingException; @@ -9,6 +9,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.synyx.beanfiller.testobjects.CopyConstructorObjectWithPrivateConstructor.COPY_CONSTRUCTOR_USED_VALUE; @@ -52,14 +53,14 @@ public void testTakesTheConstructorWithTheHighestNumberOfParameters() throws Fil assertThat(object.getBar(), notNullValue()); } - @Test(expected = FillingException.class) - public void failsWhenOnlyCopyConstructorIsAvailable() throws FillingException { + @Test() + public void failsWhenOnlyCopyConstructorIsAvailable() { JustAnotherBeanStrategy strategy = setupStrategy(); - strategy.createObject(new ObjectInformation( - CopyConstructorObject.class, null, null, null, null, null)); - } + assertThrows(FillingException.class, () -> + strategy.createObject(new ObjectInformation(CopyConstructorObject.class, null, null, null, null, null))); + } @Test public void usesPrivateConstructorOverCopyConstructor() throws FillingException { diff --git a/src/test/java/org/synyx/beanfiller/strategies/StrategyManagerTest.java b/src/test/java/org/synyx/beanfiller/strategies/StrategyManagerTest.java index 4818154..a86f41f 100644 --- a/src/test/java/org/synyx/beanfiller/strategies/StrategyManagerTest.java +++ b/src/test/java/org/synyx/beanfiller/strategies/StrategyManagerTest.java @@ -1,12 +1,12 @@ package org.synyx.beanfiller.strategies; -import org.junit.Assert; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; - +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.lessThan; @@ -27,7 +27,7 @@ public void testStrategiesInRightOrder() { int currentPriority = strategy.getPriority(); if (lastPriority != null) { - Assert.assertThat(currentPriority, is(anyOf(lessThan(lastPriority), equalTo(lastPriority)))); + assertThat(currentPriority, is(anyOf(lessThan(lastPriority), equalTo(lastPriority)))); } lastPriority = currentPriority; diff --git a/src/test/java/org/synyx/beanfiller/util/RandomGeneratorTest.java b/src/test/java/org/synyx/beanfiller/util/RandomGeneratorTest.java index 73d7e7f..3122b64 100644 --- a/src/test/java/org/synyx/beanfiller/util/RandomGeneratorTest.java +++ b/src/test/java/org/synyx/beanfiller/util/RandomGeneratorTest.java @@ -1,15 +1,12 @@ package org.synyx.beanfiller.util; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; - -import org.junit.runners.JUnit4; - -import static org.junit.Assert.fail; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.fail; /** @@ -17,12 +14,11 @@ * * @author Tobias Knell - knell@synyx.de */ -@RunWith(JUnit4.class) public class RandomGeneratorTest { private RandomGenerator sut; - @Before + @BeforeEach public void setUp() { sut = new RandomGenerator(); @@ -47,7 +43,7 @@ public void testGetIntBetweenCanReturnMinimum() { } } - Assert.assertTrue("Minimum was never returned!", hasReturnedMinimum); + assertThat(hasReturnedMinimum, is(true)); } @@ -69,7 +65,7 @@ public void testGetIntBetweenCanReturnMaximum() { } } - Assert.assertTrue("Maximum was never returned!", hasReturnedMaximum); + assertThat(hasReturnedMaximum, is(true)); }