Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing #716

Merged
merged 8 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -586,6 +597,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
34 changes: 34 additions & 0 deletions src/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Testing in Wildbook

## JUnit for Unit Testing

Wildbook uses [JUnit 5](https://junit.org/junit5/docs/current/user-guide/) for unit testing. All unit tests _must run and pass_ in order for changes to be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for backend unit testing

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`.

Ultimately, 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately Eventually

"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."

using the above naming convention.

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

TBD / linked elsewhere
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO TBD / linked elsewhere

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);
}
}
Loading