I used the above class in other parts of my code like this which feelsnicefeels nice and tidy:
public ProcessorInternalPublisher() {
internalDataStorageClient = new InternalDataStorageClient();
}
- Is the use of the static method
createDynamoDBMapperdependency injection? - How can I improve my class implementation, what are some of the standard patterns/strategies I should be using?
- Is the problem my unit test and not the class implementation?
- In the Update below I write how this refactoring now breaks another unit test which I think is proof that there is an issue with my design
public ProcessorInternalPublisher() {
String databaseTableName = System.getenv("TableName");
DynamoDBMapper mapper = InternalDataStorageClient.createDynamoDBMapper(databaseTableName);
internalDataStorageClient = new InternalDataStorageClient(mapper);
}
public class InternalDataStorageClientTest {
private InternalDataStorageClient internalDataStorageClient;
private DynamoDBMapper dynamoDBMapperMock;
@Before
public void setup() {
dynamoDBMapperMock = Mockito.mock(DynamoDBMapper.class);
when(dynamoDBMapperMock.batchSave()).thenReturn(new ArrayList<>());
internalDataStorageClient = new InternalDataStorageClient(dynamoDBMapperMock);
}
@Test
public void WHEN_SingleSuccessRequestResult_THEN_CorrectItemBatch() throws Exception {
Request correctSingleRequest = SQSTestEvents.createCorrectSingleTrainingRequest();
internalDataStorageClient.storeResults(correctSingleRequest.getEventList(), Collections.emptyList());
DBItem item = internalDataStorageClient.createDBItem(correctSingleRequest.getEventList().get(0), true);
assertThat(internalDataStorageClient.getItemBatch(), containsInAnyOrder(item));
verify(dynamoDBMapperMock, times(1)).batchSave(internalDataStorageClient.getItemBatch());
}
}
Update:
Though the solution above of using a static method allowed me to write my unit test, it broke the unit tests for my InternalPublisher class! The InternalPublisher unit tests create a InternalPublisher object and the construction fails because it is trying to use the external DB dependency.
public class InternalPublisherClientTest {
private InternalPublisher internalPublisher;
private ExternalPublisherServiceClient externalPublisher;
private InternalDataStorageClient internalDataStorageClient;
@Before
public void setup() {
internalPublisher = new InternalPublisher();
externalPublisher = Mockito.mock(ExternalPublisherServiceClient.class, RETURNS_DEEP_STUBS);
internalDataStorageClient = Mockito.mock(InternalDataStorageClient.class, RETURNS_DEEP_STUBS);
}
//Test that we do the right thing on external failure
@Test
public void WHEN_InvalidEvent_THEN_Failed_List_With_ID() {
Request request = createCorrectSingleTrainingRequest();
List<Event> failures = new ArrayList<>(request.getEventList());
//Make external dependency call fail
when(externalPublisher.newPublishServiceNotificationCall()).thenThrow(new PublisherException());
//Confirm our internal client has collected all the failures
internalPublisher.publishToPTNS(Collections.singletonList(request), externalPublisher);
assertThat(internalPublisher.getFailedRequests(), is(failures));
}
}