2016-12-08 4 views
1

필자는 Spring Mvc REST Controller 용 테스트를 작성하려고했다. https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4의 공식 문서를 따르고 있습니다. 따라서 업데이트 된 버전 to 1.4과 문서가 제시하는 것처럼 스프링 부트가 추가되었습니다.스프링 부트 1.4 - REST API 테스팅

내가 추가 한 다음 종속성

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-test</artifactId> 
</dependency> 

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope> 
</dependency> 

부모

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.2.RELEASE</version> 
    </parent> 

내 인 LoginController API를 내가 같은 외모를 테스트하려면 :

@RequestMapping(value = { "/login" }, method = RequestMethod.GET) 
    public @ResponseBody ResponseEntity<HashMap<String, Object>> login() { 


      // some logic to get Customer 
     return new ResponseEntity<>(customer, HttpStatus.OK); 

    } 

을 그리고 문서에 따라 여기 내 테스트입니다 :

@RunWith(SpringRunner.class) 
@WebMvcTest(LoginController.class) 
@SpringBootTest 
public class AuthorizationAndAuthenticationTest extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private WebApplicationContext webApplicationContext; 
    private LoginController loginController; 
    @Autowired 
    private TestRestTemplate restTemplate; 

    @MockBean 
    private LoggingService loggingService; 

    @Test 
    public void test() { 
     given(this.loggingService.logInfoMessage("some Dummy Message", this.getClass())). 
     this.restTemplate.getForObject("/login", Object.class); 
    } 
} 

이 문제는 : 1. 로그인 컨트롤러는 많은 서비스를 사용하기 때문에, 내가 "주어진"를 사용하여 모의하고 싶어하지만, 컴파일 문제를 제공합니다 내가 확실하지하지만 일부 종속성을 잃었처럼 보인다. 2.TestRestTemplate은 더 이상 사용되지 않으며 대안은 무엇입니까? 나는 대안을 찾지 못했다.

이것은 처음으로 스프링 프레임 워크를 사용하여 테스트를 작성하므로 몇 분 세부 사항을 간과 할 수 있습니다.

도와주세요. 문제 # 2에 관한

given(this.loggingService.logInfoMessage("some Dummy Message", this.getClass())) 
    .willReturn("your desired value") 

: org.springframework.boot.test.TestRestTemplateorg.springframework.boot.test.web.client.TestRestTemplate 찬성되지 않습니다

답변

3

는 문제 # 1에 관하여 : logInfoMessage 스텁하는 동안 당신은 아마 to call .withReturn을 잊고있다.

패키지를 변경하십시오.

현재 Spring MVC 끝점을 테스트하는 데 가장 적합한 구조는 TestRestTemplate이 아닌 MockMvc입니다.

관련 문제