2016-05-12 63 views
0

我有一個服務/ dao層。服務層方法調用我從中獲得響應的dao的第一個方法,並調用dao中的第二個方法來傳遞一些參數,包括1st dao方法的響應中的值。我嘗試使用模擬,但它與空指針失敗。 僞代碼是一樣的東西如下:嘲笑連續的REST呼叫

Service{ 
serviceMethod(some_args){ 
response1 = dao.method1(some_args); 
someItem = response1.get("someItem"); 

/* do some logic on someitem to create otherItem*/ 
request2.setArgs(someItem); 
response2 = dao.method2(request2); 
} 
} 

我曾嘗試如下嘲笑,但它不工作。

@Test 
     public void testPass(){ 
     mockResponse1 = new Response1(); 
     mockRequest2 = new MockRequest2(); 
     when(dao.method1(some_args)).thenReturn(mockResponse1) 
     mockResponse1.setArgs(some_args); 
     mockRequest2.setArgs(mockResponse1.getargs()); 
     mockResponse2 = new Response2(); 
     when(dao.method2(mockRequest2)).thenReturn(mockResponse2) 
     service.serviceMethod(some_args) 
     } 
+1

這是一個真正的Java代碼,或只是一個僞代碼?我看到mockResponse1是一個實際的實例,mockResponse2也是一個實際的實例。如果你打電話給這些嘲笑,我期待像模擬(Response1.class)和模擬(Response2.class)。如果你真的沒有注意到這一點,那麼你的問題。 –

+0

''代碼是''可能會讓你難以發現問題,當你不包含實際的模擬創建代碼(= dao'),並且還包括代碼將模擬注入到服務類中的代碼。此外'mockRequest2'沒有在你的測試代碼中的任何地方定義 –

+0

這是僞代碼 –

回答

0

你可以使用一個ArgumentCaptor來獲取傳遞到dao.method2(...)值,然後使上斷言。

例如,說我有這個DAO ...

public interface DAO { 
    Response method1(Request request);  
    Response method2(Request request); 
} 

而這個服務...

public class Service { 

    private DAO dao; 

    public void setDao(DAO dao) { 
     this.dao = dao; 
    } 

    public Response serviceMethod(Request someArgs) { 
     Response response1 = dao.method1(someArgs); 
     String someItem = response1.getTheResponse(); 
     Request request2 = new Request(someItem); 
     return dao.method2(request2); 
    } 
} 

,此服務的測試可能是......

import static org.fest.assertions.Assertions.assertThat; 
import static org.mockito.Matchers.any; 
import static org.mockito.Mockito.verify; 
import static org.mockito.Mockito.when; 

import org.junit.Before; 
import org.junit.Test; 
import org.mockito.ArgumentCaptor; 
import org.mockito.Captor; 
import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.MockitoAnnotations; 

public class ServiceTest { 

    @Mock 
    private Request request; 

    @Mock 
    private Response response; 

    @Mock 
    private Response serviceResponse; 

    @Mock 
    private DAO dao; 

    @InjectMocks 
    private Service service; 

    @Captor 
    private ArgumentCaptor<Request> requestCaptor; 

    @Before 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 
    } 

    @Test 
    public void shouldDoServiceMethod() { 
     // Set up 
     when(dao.method1(request)).thenReturn(response); 
     when(response.getTheResponse()).thenReturn("[ARGUMENT]"); 
     when(dao.method2(any(Request.class))).thenReturn(serviceResponse); 

     // Code under test 
     Response actualResponse = service.serviceMethod(request); 

     // Verification 
     assertThat(actualResponse).isSameAs(serviceResponse); 

     verify(dao).method2(requestCaptor.capture()); 

     Request actualSecondRequest = requestCaptor.getValue(); 

     assertThat(actualSecondRequest.getArgs()).isEqualTo("[ARGUMENT]"); 
    } 
} 

重點行...

verify(dao).method2(requestCaptor.capture()); 

這驗證了method2被調用並捕獲了它被調用的值。

那麼你得到的價值...

Request actualSecondRequest = requestCaptor.getValue(); 

...然後你可以驗證相關的信息被設置...

assertThat(actualSecondRequest.getArgs()).isEqualTo("[ARGUMENT]"); 

希望這有助於。


爲了完整起見,這裏的RequestResponse ...

public class Request { 

    private String args; 

    public Request(String args) { 
     this.args = args; 
    } 

    public String getArgs() { 
     return args; 
    } 
} 

public class Response { 

    private String theResponse; 

    public Response(String theResponse) { 
     this.theResponse = theResponse; 
    } 

    public String getTheResponse() { 
     return theResponse; 
    } 
}