2016-09-21 12 views
2

ich unter diesem Link sah: How to write a unit test for a Spring Boot Controller endpointUnit Testing - Frühling Boot-App

ich Einheit bin der Planung testen mein Frühling Boot-Controller. Ich habe unten eine Methode von meinem Controller eingefügt. Wenn ich den im obigen Link genannten Ansatz nutze, wird der Anruf, den ich habe, service.verifyAccount (Anfrage) nicht gemacht? Testen wir nur, ob der Controller die Anforderung im angegebenen Format akzeptiert und die Antwort in einem anderen Format als dem Testen der HTTP-Statuscodes zurückgibt?

@RequestMapping(value ="verifyAccount", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE) 
public ResponseEntity<VerifyAccountResponse> verifyAccount(@RequestBody VerifyAccountRequest request) { 

    VerifyAccountResponse response = service.verifyAccount(request); 

    return new ResponseEntity<VerifyAccountResponse>(response, HttpStatus.OK); 
} 
+1

Es hängt davon ab, ob Sie das Serviceobjekt verspottet haben. Wenn Sie nicht verspottet haben, würde es den Dienst anrufen. – notionquest

+0

Dank @notionquest. Was ist der Zweck von MockMvc und wenn ich keine Mock-Objekte verwende, werden alle meine Abhängigkeiten durch die Verwendung des Codes (akzeptierte Antwort) in dem anderen Beitrag injiziert? –

+0

Vielleicht ist dieser Beitrag http://stackoverflow.com/questions/32223490/are-springs-mockmvc-used-for-unit-testing-or-integration-testing hilfreich für die Beantwortung Ihrer Frage "was ist der Zweck von MockMvc". –

Antwort

0

Sie Unit-Test-Fall mit

@RunWith(SpringJUnit4ClassRunner.class) 
    // Your spring configuration class containing the        
    @EnableAutoConfiguration 
    // annotation 
    @SpringApplicationConfiguration(classes = Application.class) 
    // Makes sure the application starts at a random free port, caches it   throughout 
    // all unit tests, and closes it again at the end. 
    @IntegrationTest("server.port:0") 
    @WebAppConfiguration 

schreiben sicherstellen, dass Sie alle Ihre Serverkonfiguration wie Port/url

@Value("${local.server.port}") 
private int port; 


private String getBaseUrl() { 
    return "http://localhost:" + port + "/"; 
} 

Dann Code unten

erwähnt verwenden konfigurieren
 protected <T> ResponseEntity<T> getResponseEntity(final String  
    requestMappingUrl, final Class<T> serviceReturnTypeClass, final Map<String, ?>  
    parametersInOrderOfAppearance) { 
    // Make a rest template do do the service call 
    final TestRestTemplate restTemplate = new TestRestTemplate(); 
    // Add correct headers, none for this example 

    final HttpEntity<String> requestEntity = new HttpEntity<String>(new  
    HttpHeaders()); 
    // Do a call the the url 
    final ResponseEntity<T> entity = restTemplate.exchange(getBaseUrl() +  
    requestMappingUrl, HttpMethod.GET, requestEntity, serviceReturnTypeClass,  
    parametersInOrderOfAppearance); 
    // Return result 
    return entity; 
    } 

@Test 
public void getWelcomePage() { 
    Map<String, Object> urlVariables = new HashMap<String, Object>(); 
    ResponseEntity<String> response = getResponseEntity("/index",  
    String.class,urlVariables); 

    assertTrue(response.getStatusCode().equals(HttpStatus.OK)); 
}