我我的春天啓動的項目更新從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