2017-04-19 59 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()); 
    } 
} 

也试过@Autowire的TestRestTemplate并省略主机名和端口的请求。

+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()); 
    } 
}