2017-04-19 7 views
1

내 스프링 부트 프로젝트를 1.3.x에서 1.5.2로 업데이트했습니다. 테스트 프레임 워크가 "변경"되어 코드를 마이그레이션하려고합니다. RestTemplate의 응답 상태 코드는 401이어야하지만 새로운 "구조"로 코드를 변경하면 404가 발견되지 않습니다. 어떤 아이디어가 누락되었을 수 있습니까?봄 부팅 테스트, 1.3에서 1.5로 마이그레이션

오래된 코드 :

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = ApiAuthServerApplication.class) 
@WebAppConfiguration 
@IntegrationTest("{server.port:0, server.address:localhost}") 
public class ApiEndpointTests { 

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

    private RestTemplate template = new TestRestTemplate(); 

    @Test 
    public void clientsEndpointProtected() { 
     ResponseEntity<String> response = template.getForEntity("http://localhost:" 
       + port + "/uaa/api/v1/oauth/clients", String.class); 
     assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());    
    } 
} 

새로운 코드는 내가 노력 :

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class ApiEndpointTests { 

    @LocalServerPort 
    private int port; 

    private TestRestTemplate template = new TestRestTemplate(); 

    @Test 
    public void clientsEndpointProtected() { 
     ResponseEntity<String> response = template.getForEntity("http://localhost:" 
       + port + "/uaa/api/v1/oauth/clients", String.class); 
     assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); 
    } 
} 

는 또한 TestRestTemplate @Autowire에 노력하고 요청에 호스트 이름과 포트를 생략합니다.

+1

당신은'Autowire'은'TestRestTemplate' 대신 새로운 인스턴스를 생성 @, 봄 부팅 후 제대로 URL을 수정해야 할 필요가해야 당신은/UAA/API'호출 할 수 있습니다/v1/oauth/clients'에 있습니다. –

+0

이전에 시도한 결과와 동일한 결과가 나타납니다. – Filip

답변

2

WebEnvironment.RANDOM_PORT를 사용할 때 스프린트 테스트 프레임 워크는 호스트와 포트의 구성 및 설정을 처리합니다. 따라서 호스트 및 포트의 세부 사항을 제거 할 수 있습니다. 또한 TestRestTemplate에서 @Autowired 주석을 사용해야합니다.

@Autowired for the TestRestTemplate. 

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class ApiEndpointTests { 

    @Autowired 
    private TestRestTemplate template = new TestRestTemplate(); 

    @Test 
    public void clientsEndpointProtected() { 
    ResponseEntity<String> response = 
    template.getForEntity("/uaa/api/v1/oauth/clients", String.class); 
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); 
    } 
} 
+0

이전과 같은 결과 (404 찾을 수 없음)가 responsecode에 있습니다. – Filip

1

알아 냈어.

이 코드를 사용하는 경우는 :

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class ApiEndpointTests { 

    @Autowired 
    private TestRestTemplate template; 

    @Test 
    public void clientsEndpointProtected() { 
    ResponseEntity<String> response = 
    template.getForEntity("/uaa/api/v1/oauth/clients", String.class); 
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); 
    } 
} 

또한이 컨텍스트 경로이기 때문에 /uaa을 제거 할 필요가 있었다. 나는 그것도 자동으로 TestRestTemplate을 포함하는 것 같아요. 그래서 일 최종 코드 :

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = ApiAuthServerApplication.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class ApiEndpointTests { 

    @Autowired 
    private TestRestTemplate template; 

    @Test 
    public void clientsEndpointProtected() { 
    ResponseEntity<String> response = 
    template.getForEntity("/api/v1/oauth/clients", String.class); 
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); 
    } 
}