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