Skip to content

Commit

Permalink
Merge pull request #716 from WildMeOrg/unit_test_init
Browse files Browse the repository at this point in the history
Unit testing
  • Loading branch information
TanyaStere42 authored Sep 7, 2024
2 parents 53f133b + 5b47d5c commit c046540
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
31 changes: 27 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,24 @@

<!-- end local repo dependencies-->

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0-M1</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.geotools</groupId>
Expand Down Expand Up @@ -559,6 +570,18 @@
<version>3.6.1</version>
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
Expand Down
35 changes: 35 additions & 0 deletions src/test/README.md
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions src/test/java/org/ecocean/AnnotationTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c046540

Please sign in to comment.