Geode is tested with a combination of tests written with JUnit and DUnit. JUnit tests are written as with any other project while DUnit tests use a multi-JVM environment (see DistributedTest).
JUnit tests are divided into two main categories: UnitTest and IntegrationTest.
1. UnitTest: is a test with very narrow and well defined scope. Any complex dependencies and interactions are stubbed or mocked.
- Should use JUnit 4 syntax
- File name ends with *Test
- Should be part of the test source set folder (<geode-module-dir>/test/java)
- Should complete in milliseconds
- Should generally test a single class
- Typically uses white-box testing, uses Mocks/Fakes and helps guarantee internal quality (quality of code and class design)
- Follows "A Set of Unit Testing Rules" by Michael Feathers
- A UnitTest should not do any of the following:
- communicate with a database
- communicate across the network
- access the file system
- prevent the running of other unit tests in parallel
- require anything special in the environment (such as editing config files or running an external process)
- A UnitTest should not do any of the following:
2. IntegrationTest: a test involving inter-operation of components or subsystems.
- Should use JUnit 4 syntax
- File name ends with *IntegrationTest
- Should be part of the integrationTest source set folder (<geode-module-dir>/integrationTest/java)
- May take longer than a UnitTest but should generally complete in seconds
- May test a single class or any combination of classes including end-to-end testing of Geode
- Typically uses black-box testing but may include some white-box testing and helps guarantee external quality (feature delivers value to User)
- An IntegrationTest may do any of the following:
- communicate across the network
- access the file system
- require anything special in the environment (such as editing config files or running an external process)
DUnit tests are a special type of JUnit test involving multiple JVMs. This supports writing tests for a Geode cluster.
3. DistributedTest: a test involving multiple members of a distributed system.
- Should use JUnit 4 syntax
- File name ends with *DistributedTest or *DUnitTest
- Should be part of the distributedTest source set folder (<geode-module-dir>/distributedTest/java)
- Should generally complete in seconds
- May test interactions between multiple JVMs in a Geode cluster or via networking or via file system
- Typically uses black-box testing but may include some white-box testing and helps guarantee external quality (feature delivers value to User)
- A DistributedTest may do any of the following:
- communicate across the network
- access the file system
- require anything special in the environment (such as editing config files or running an external process)
Regression tests are typically an IntegrationTest or DistributedTest written to reproduce a specific bug and verify the fix of that bug.
4. RegressionTest: a UnitTest, IntegrationTest or DistributedTest that reproduces a specific bug and verifies its fix.
- Should use JUnit 4 syntax
- File name ends with *RegressionTest
- Should use Category annotation of type UnitTest, IntegrationTest or DistributedTest
- Should follow the guidelines of its Category
- Should have Javadocs on the test class that reference the bug and describe it sufficiently without having to look up the bug in JIRA
- May use black-box or white-box testing to guarantee that a bug has been fixed and will not be reintroduced without causing this test to fail
The primary testing frameworks and libraries used in Geode tests (all of the above test categories) are:
- JUnit 4
- System Rules – powerful set of JUnit 4 Rules
- AssertJ – richer than JUnit 4 Assert and includes better support for expected exceptions
- Awaitility – useful for awaiting asynchronous conditions
- Mockito – preferred over all other mocking libraries
- JUnitParams – provides test method parameterization
- Catch-Exception – better support for expected exceptions especially with complex nesting of exceptions or exceptions with unusual APIs
- PowerMock – use this as a last resort – refactoring code to facilitate better testing is preferred
- Custom JUnit 4 Rules and testing frameworks (including DUnit) that are part of Geode
Always write unit tests for new functionality and always ensure that all unit tests pass before submitting a pull request. Try to make sure new functionality is covered by unit tests, whether or not there are also integration tests as well.
The main build target will execute UnitTests only:
./gradlew build
Our ultimate goal is to have the highest quality product for our users. There are multiple ways of slicing this issue, but if we go all the way back to when the code is being developed, we find unit tests. Unit tests are the foundation in continuous software testing, with the top characteristic of being automated. Good unit testing can save a lot of time and energy later on in the software development lifecycle.
Possible goals to strive for when creating our tests: (from http://artofunittesting.com/definition-of-a-unit-test/)
- Able to be fully automated
- Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed)
- Can be run in any order if part of many other tests
- Runs in memory (no DB or File access, for example)
- Consistently returns the same result (You always run the same test, so no random numbers, for example. save those for integration or range tests)
- Runs fast
- Tests a single logical concept in the system
- Readable
- Maintainable
- Trustworthy (when you see its result, you don't need to debug the code just to be sure)
Good sites for getting ideas: