2017-04-19 6 views
1

私のspring-bootプロジェクトを1.3.xから1.5.2にアップデートしました。テストフレームワークは「変更されました」と私のコードを移行しようとしています。 RestTemplateの応答ステータスコードは401でなければなりませんが、コードを新しい「構造体」に変更すると404が見つかりません。何が欠けているかもしれないアイデア?Springブートテスト、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を使用すると、sprintテストフレームワークはホストとポートの構成とセットアップを処理します。したがって、ホストとポートの詳細を削除することができます。また、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

responseCodeに以前と同じ結果が得られました(404 Not found) – 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()); 
    } 
} 
関連する問題