我我的春天启动的项目更新从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并省略主机名和端口的请求。
您应该只需要'@ Autowire'了'TestRestTemplate'而不是创建一个新的实例,春季应启动正常,然后修复URL,你可以调用'/ UAA/API/V1/OAuth的/ clients'。 –
我已经尝试过这一点,我也得到相同的结果 – Filip