Reputation: 476
The lines of code that I want to test is:
private static final EntityType WORK_FLOW_ENTITY_TYPE = //Assigned
public WorkflowRequest getWFRequestFromHerdInput(HerdInput herdInput) throws
NonRetriableException {
ActionRequest request = CoralHerdUtils.getRequestData(herdInput);
Document document = request.getHerdDocument();
List<Entity> entityList = document.getEntitiesByType(WORK_FLOW_ENTITY_TYPE);
Entity entity = entityList.get(0);
WorkflowRequest workflowRequest = null;
try {
workflowRequest = (WorkflowRequest) entity.asCommonsObject();
} catch (DocumentException e) {
throw new NonRetriableException("Object cannot be converted to WorkflowRequest");
}
return workflowRequest;
}
I want to test the catch part of the try-catch. I am using PowerMockito and Mockito both. For testing, I have written the following:
@Test(expected = NonRetriableException.class)
public void test_GetWFRequestFromHerdInput_fail() throws NonRetriableException, DocumentException {
PowerMockito.mockStatic(CoralHerdUtils.class);
PowerMockito.when(CoralHerdUtils.getRequestData(herdInput)).thenReturn(actionRequest);
EntityType WORKFLOW_ENTITYTYPE = new EntityType(new Aspect("DigitalInfluence"),
"Application", "1.0");
DocumentFactory docFactory = new DocumentFactory();
docFactory.registerCommonsClass(WorkflowRequest.class, WORKFLOW_ENTITYTYPE);
document = docFactory.createDocument();
document.addEntity(workflowRequest);
Mockito.when(actionRequest.getHerdDocument()).thenReturn(document);
List<Entity> entities = Mockito.mock(List.class);
Entity entity = Mockito.mock(Entity.class);
entities.add(entity);
Document documentMock = Mockito.mock(Document.class);
Mockito.when(documentMock.getEntitiesByType(WORKFLOW_ENTITYTYPE)).thenReturn(entities);
Mockito.when(entities.get(0)).thenReturn(entity);
Mockito.when(entity.asCommonsObject()).thenThrow(DocumentException.class);
WorkflowRequest workflowRequestReturned = herdDocumentHelper.getWFRequestFromHerdInput(herdInput);
Assert.assertEquals(EXPECTED_DAG_ID, workflowRequestReturned.getDagId());
}
The problem is the test case is not picking the mocked Entity object but entityList.get(0)
which is created inside the method.
How can I forcefully inject the mocked object in the method so that I can test the catch branch?
Upvotes: 0
Views: 672
Reputation: 140467
The real answer is: you have to understand what mocking is and how it works.
Just creating a mock object somewhere doesn't magically "get" that object into your code under test.
Thus a distinct non-answer: learn how mocking frameworks work, and then apply that to your current code base (as outlined in the answer by user talex). Start reading here for example.
Btw: so far, nothing in your production code justifies to use PowerMock(ito). So, if possible, stay with plain Mockito.
Upvotes: 1
Reputation: 20464
You need to mock:
CoralHerdUtils.getRequestData
to return mocked ActionRequest
request.getHerdDocument
to return mocked Document
document.getEntitiesByType
to return list containing your mocked entity
Upvotes: 1