Cheat Sheet
Along the way we learned a few lessons that makes testing easier.
- Create a directory in
/src/test/resources/ut/[yourClassName-no extension]where you will store all the test data you want to process. UseKeepTestUtils.getResourceAsJson("file.json",YourTestClass.class)to retrieve values - Annotate your class with
@UnitTest,@IntegrationTestor@PerformanceTest. The first one runs onmvn packagethe second one runs onmvn verifyand the last one only runs on manual. - Use the
@Mockannotation instead ofmock(classname) - After the mock definitions, add the Rule using
@Rule MockitoRule mockitoRule = MockitoJUnit.rule(); - Keep this sequence:
- Initialize your Mocks in
@BeforeEach, usingMockitoAnnotations.initMocks(this); - Initialize your local (non mock) variables;
- Define mock behavior. If you switch 2 and 3, be ready for errors.
- Initialize your Mocks in
- Use
KeepTestSubscriber[Json](see Test Helpers) - Use
KeepTestUtils - If suitable, extend
AbstractDatabaseHandlerTest
Code example
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import com.hcl.domino.data.Document;
import com.hcl.domino.keep.Constants;
import com.hcl.domino.keep.cache.NameLookupCache;
import com.hcl.domino.keep.exceptions.KeepExceptionMissingParameters;
import com.hcl.domino.keep.testhelper.AbstractDatabaseHandlerTest;
import com.hcl.domino.keep.testhelper.KeepTestUtils;
import com.hcl.domino.keep.testhelper.KeepTestUtils.TestType;
import com.hcl.domino.security.AclEntry;
import com.hcl.tests.UnitTest;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
@UnitTest
@SuppressWarnings("resource")
class CreateTaskTest extends AbstractDatabaseHandlerTest {
@Mock
Document note;
@Mock
AclEntry aclEntry;
@Mock
NameLookupCache nameLookupCache;
CreateTask request;
@Override
@BeforeEach
protected void beforeEach(final Vertx vertx) {
super.beforeEach(vertx);
this.request = new CreateTask();
this.factory.setLookupCache(this.nameLookupCache);
}
@Test
void testEmptyPayload() throws KeepExceptionMissingParameters {
this.expectFailure(Exception.class, this.request, this.getJsonParams());
}
@Test
void testProcessPayloadSuccess() throws KeepExceptionMissingParameters, Exception {
this.subscriber.minimumOnNextCount(1);
this.request.setSubscriber(this.subscriber);
final JsonObject inputPayload = KeepTestUtils.getJsonFromResource(TestType.UNIT_TEST,
"/testCreateUpdateTask.json", CreateUpdateTaskTest.class);
this.incoming.put(Constants.REQUEST_BODY, inputPayload);
Mockito.when(this.s.createDocumentFromJson(ArgumentMatchers.any(), ArgumentMatchers.any()))
.thenReturn(this.note);
this.request.process(this.getJsonParams());
}
}