如何验证父类中的受保护方法是否已运行?

时间:2016-02-17 19:34:02

标签: java testing mocking mockito verify

当我在父类中存在发送响应的方法时,我试图捕获子控制器类的响应字符串。

我已经尝试过模拟父类并验证方法是否被调用但是由于该方法在类中被引用,因此验证没有提到正在调用消息传递方法。如何验证是否已调用此方法以便捕获响应内容?

这是我的代码的抽象版本,但它解释了我相信的情况。

public class AbstractController {
    public void handleRequest(HttpServletResponse response, 
         String forward, HttpMethod method, MessageRequest request){
        //the method process request then sends response back using this
        sendJsonResponse(response,HttpStatus.CREATED,messageResponse);
    }

    protected  sendJsonResponse(HttpServletResponse response, HttpStatus status, String msg){
        //send the message 
    }
}

public class StartController extends AbstractController {
    public void start(String arg1, String arg2, String arg3, HttpSevletResponse response){
        handleRequest(reponse,"actor1",HttpMethod.POST,new MessageRequest(arg1,arg2,arg3));
    }
}

public class TestStartController{
    // several mocked services declared here
    @Test
    public void testStart(){

        String expectResult = "some string";

        when(mockservc.sendMessageDeeper(~)).thenReturn("raw response"); 

        ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);

        StartController sc = new StartController(); 

        sc.start(anyString(),anyString(),anyString,(),responseMock);

        verify(mockServc).sendMessageDeeper(~);

        //this is the method that I need to make sure runs since the method below is 
        //statically reference it is not registering being executed by the verify need help here             
        verify(abstractControllerMock).sendJsonReponse(same(responeMock),eq(HttpStatus.CREATED),bodyCaptor.capture()); 

        assertJsonEquals(expectResult,bodyCaptor.getValue());
    }
}

0 个答案:

没有答案