Test Helpers
We use a set of helpers to make testing easier:
- Mockito to mock objects (see Testing
- Custom annotations:
@UnitTest
,@IntegrationTest
and@PerformanceTest
- Class KeepTestUtils: retrieve test specific sources
- Classes TestSubscriberGeneric, TestSubscriberJson for Assertions when extending
AbstractAsycDomino
Use of TestSubscriberJson
As example for the whole set of TestSubscribers. The TestSubscriber can be used to expect success or error for the specific function under test. Expected outcomes are added fluently to the test subscriber. Not each combination makes sense.
TestSubscriberJson subscriber = new TestSubscriberJson();
Testing failure
When we expect failure we have 3 methods to indicate this:
subscriber.expectFailure(true);
Expectation is that the code under test will call the onError()
function and not onComplete()
without further specifying why. Test fails if onComplete()
runs
subscriber.errorClass(KeepException.class)
Expectation that onError()
gets called with a specific error class. Test fails if a different Error is raised or onComplete()
runs
subscriber.errorMessage("A specific error message")
Expectation that onError()
is called and the Error has the specified message. Fails if the message is different or onComplete()
runs
Testing success
There are various switches to test success. By default a TestSubscriber expects onComplete()
to run and not onError()
subscriber.expectedOnNextCount(7)
Expect exactly 7 calls to onNext()
and one call to onComplete()
, will fail with less or more calls
subscriber.minimumOnNextCount(2)
Expect two or more calls to onNext()
and one call to onComplete()
, will fail with less calls. Typical use with 1 to ensure at least one result is returned
subscriber.maximumOnNextCount(2)
Expect zero, one or two calls to onNext()
and one call to onComplete()
, will fail with more calls
subscriber.expectEmpty(true)
WHen set a call to onNext()
can be an empty object {}
. If not set onNext("{}")
fails
subscriber.addfieldToExist("SomeField").addfieldToExist("SomeOtherField")
Expect every call to onNext()
contains a JSON element with the given field names, no assertion on data type is made
subscriber.addExpectedValue(Json1).addExpectedValue(Json2)
Adds expeced JSON objects for the first and second call to onNext()
. Fails if other values arrive. Exact behavior can be tuned using additional methods:
subscriber.failOnAdditionalFieldsInActual(false)
Ignore additional fields returned in a onNext()
call. Just check that fields specified in addFieldsToExist()
and/or addexpectedValue()
are present
subscriber.ignoreMissingFieldsInActual(true)
Pass test even if some fields in actual are missing