diff --git a/pom.xml b/pom.xml index 3a3339bd31..00179dd6dc 100644 --- a/pom.xml +++ b/pom.xml @@ -373,13 +373,24 @@ - - + org.junit.jupiter junit-jupiter-api - 5.8.0-M1 + 5.11.0 test - + + + org.junit.jupiter + junit-jupiter-engine + 5.11.0 + test + + + org.junit.platform + junit-platform-launcher + 1.11.0 + test + org.geotools @@ -559,6 +570,18 @@ 3.6.1 + + maven-surefire-plugin + 2.19 + + + org.junit.platform + junit-platform-surefire-provider + 1.0.0 + + + + org.codehaus.mojo exec-maven-plugin diff --git a/src/test/README.md b/src/test/README.md new file mode 100644 index 0000000000..65a33e2632 --- /dev/null +++ b/src/test/README.md @@ -0,0 +1,35 @@ +# Testing in Wildbook + +## JUnit for Unit Testing + +Wildbook uses [JUnit 5](https://junit.org/junit5/docs/current/user-guide/) for backend unit testing. All unit tests _must run and pass_ in order for changes to be +incorporated into the Wildbook codebase. Tests should be run automatically with maven when executing `mvn clean install`. + +**All new code** should have appropriate unit tests written to validate all components of the new code are working as expected. Take care to also test +invalid data cases and failure/exception cases. + +Tests should be created under the `src/test/java/org/ecocean/` directories, corresponding to the java class which are testing. The test class name should follow +the convention of adding the suffix `Test` to the java class name, such as `AnnotationTest.java` for tests of code within `Annotation.java`. + +Eventually, almost all Wildbook java classes will have a corresponding `*Test.java` class. If one does not exist to add new tests to, it should be created +using the above naming convention. That being said, edits to existing classes may not require full understanding of a class, while testing does. If you choose +not to make a unit test for an existing class, make a note of that choice in the PR and explain why. + +There are many tutorials and guides to using JUnit online, such as [this one](https://www.vogella.com/tutorials/JUnit/article.html). + +### Running tests without compiling + +To test only specific test classes/methods, you can use the following with maven: + +``` +mvn test # run all tests but no compiling + +mvn test -Dtest=TestClass1,TestClass2 # only these two classes + +mvn test -Dtest=TestClass#method # test a specific method +``` + + +## Integration Testing, Frontend Testing + +TODO diff --git a/src/test/java/org/ecocean/AnnotationTest.java b/src/test/java/org/ecocean/AnnotationTest.java new file mode 100644 index 0000000000..8fd67f6907 --- /dev/null +++ b/src/test/java/org/ecocean/AnnotationTest.java @@ -0,0 +1,24 @@ +package org.ecocean; + +import org.ecocean.Annotation; +import org.ecocean.media.*; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; +import static org.junit.Assert.*; + +class AnnotationTest { +/* + to create a (real) MediaAsset or a Feature we need to have a Shepherd! + TODO extend to this with mockito/Shepherd + https://semaphoreci.com/community/tutorials/stubbing-and-mocking-with-mockito-2-and-junit + */ + @Test void createAnnotation() { + AssetStore store = null; // see note above + MediaAsset ma = new MediaAsset(store, null); + Annotation ann = new Annotation("species", ma); + + assertNotNull(ann); + assertTrue(ann.isTrivial()); + assertEquals(ann.getMediaAsset(), ma); + } +}