Reputation: 299
I'm trying to cover a test class to 100%. However, I'm able to do it up to 90% only as I'm not able to test catch block. Can anyone tell me how can I cover the catch block.
Below is my class to be tested:
public class UnsubscribeXmlTransformer {
public Map<String, Object> process(Map<String, Object> requestMap) {
String inputStream = requestMap.get(NeoConstants.INPUTSTREAM).toString();
Smooks smooks;
try {
smooks = new Smooks("Unsubscribe-smooks.xml");
JavaResult javaResult = new JavaResult();
smooks.filterSource(new StringSource(inputStream), javaResult);
UnsubscribeDetailsVO unsubscribeDetailsVO = (UnsubscribeDetailsVO) javaResult
.getBean("unsubscribeDetailsVO");
requestMap.put("unsubscribeDetailsVO", unsubscribeDetailsVO);
} catch (IOException | SAXException e) { }
return requestMap;
}
}
And here is my Test class:
public class UnsubscribeXmlTransformerTest {
Map<String, Object> requestMap = null;
@Before
public void prepareRequestMap() {
requestMap = new HashMap<>();
String inputStream = "some xml tags";
requestMap.put(NeoConstants.INPUTSTREAM, inputStream);
}
@Test
public void processTest() throws Exception {
UnsubscribeXmlTransformer ref = new UnsubscribeXmlTransformer();
Map<String, Object> result = ref.process(requestMap);
Assert.assertNotNull("Result is not null", result);
Assert.assertFalse("Result is not empty", result.isEmpty());
Assert.assertTrue("Result contains key named inputStream", result.containsKey(NeoConstants.INPUTSTREAM));
Assert.assertTrue("Result contains key named unsubscribeDetailsVO", result.containsKey("unsubscribeDetailsVO"));
Assert.assertTrue("Value with key unsubscribeDetailsVO is an instance of UnsubscribeDetailsVO", result.get("unsubscribeDetailsVO") instanceof UnsubscribeDetailsVO);
}
@Test(expected = IOException.class)
public void processTestForException() {
UnsubscribeXmlTransformer ref = new UnsubscribeXmlTransformer();
// how to cover
}
}
Upvotes: 3
Views: 17202
Reputation: 171
Here when we execute classUnderTest.handleError(clienthttpresponse); it will throw Exception in java class ..inorder to coverage the code in catch block in java class we need to write the below code in try block and Assert.fail() , then instead of right away throwing the error it will go into catch block.
try {
Mockito.when(clienthttpresponse.getBody()).thenReturn(targetStream);
classUnderTest.handleError(clienthttpresponse);
fail();
}catch(Exception e) {
System.out.println(e.getClass()+""+e.getMessage());
}
Upvotes: 0
Reputation: 72
You can use Mockito.spy
on your requestMap, and mock case, when exception should be thrown. Then (as pointed by jannis) you can use AssertJ's fluent API to verify there's no exception thrown (you are muting it in catch
clause), e.g
@Test
public void process_shouldCatchException() throws Exception{
UnsubscribeXmlTransformer sut = new UnsubscribeXmlTransformer();
Map<String, Object> requestMap = Mockito.spy(new HashMap<>());
when(requestMap.put(eq("unsubscribeDetailsVO"), any()))
.thenThrow(IOException.class);
assertThatCode(() -> sut.process(requestMap))
.doesNotThrowAnyException();
}
Upvotes: 4