Sunday, 22 March 2015

Mocking Non Void Public Method

@Test
 public void testnonVoidPublicMethod() throws Exception
 {
  
  
  Helper helper = PowerMockito.mock(Helper.class);
  
   //PowerMockito.mockStatic(Helper.class);
  //ArgumentCaptor captor = ArgumentCaptor.forClass(StringBuilder.class);

  /**
   * Mocking the result of Public Non Void Instance method
   * Returning Result as an Answer implementation
   * 
   */
  
  //PowerMockito.doAnswer(answerIsArgumentString()).when(helper,"nonVoidPublicMethod",
    entity,stringBuilder);
   
  /**
   * Making the mocked behaviour of the method to throw an Exception
   * 
   */
  
  PowerMockito.doThrow(new RuntimeException(" Mocked Exception ")).when(helper
,"nonVoidPublicMethod",entity,stringBuilder); String ok= helper.nonVoidPublicMethod(entity,stringBuilder.append("ok Subhankar")); /** * We can keep Assert statements Here * */ /** * End of Assert Statements */ } //This method returns the toString() version of the second argument of the method invoked private static Answer answerIsArgumentString() { return new Answer() { public String answer(InvocationOnMock invocation) { String str = (invocation.getArguments()[1].toString()); return str; } }; } //This method returns the toString() version of the first argument of the method invoked private static Answer answerIsArgumentDate() { return new Answer() { public String answer(InvocationOnMock invocation) { String str = (invocation.getArguments()[0]).toString(); return str; } }; }

NEXT

View Subhankar Paul's profile on LinkedIn

No comments:

Post a Comment