如何在SpringBootTest中将基本auth添加到Autowired testRestTemplate; Spring Boot 1.4

Tuh*_*rma 6 spring-mvc spring-security spring-test-mvc spring-boot

我在Spring Boot 1.4之前进行的OAuth集成测试如下所示(更新只是为了不使用已弃用的功能):

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ApplicationConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {

    @Value("${local.server.port}")
    private int port;

    private static final String CLIENT_NAME = "client";
    private static final String CLIENT_PASSWORD = "123456";

    @Test
    public void testOAuthAccessTokenIsReturned() {
        MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
        request.set("username", "user");
        request.set("password", password);
        request.set("grant_type", "password");
        @SuppressWarnings("unchecked")
        Map<String, Object> token = new TestRestTemplate(CLIENT_NAME, CLIENT_PASSWORD)
            .postForObject("http://localhost:" + port + "/oauth/token", request, Map.class);
        assertNotNull("Wrong response: " + token, token.get("access_token"));
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在想要使用如此处所述的Autowired TestRestTemplate http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-应用程序-工作-与随机端口

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
    ApplicationConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {

    private static final String CLIENT_NAME = "client";
    private static final String CLIENT_PASSWORD = "123456";

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Test
    public void testOAuthAccessTokenIsReturned() {
        MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
        request.set("username", "user");
        request.set("password", password);
        request.set("grant_type", "password");
        @SuppressWarnings("unchecked")

        Map<String, Object> token1 = this.testRestTemplate. //how to add basic auth here
        assertNotNull("Wrong response: " + token, token.get("access_token"));
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为这是添加auth的最接近的方式:

使用RestTemplate进行Spring 4.0.0基本身份验证

我想使用Autowired testRestTemplate来避免在我的测试中解析主机和端口.有没有办法做到这一点?

Tuh*_*rma 12

这在Spring Boot 1.4.1中得到了修复,它有一个额外的方法

testRestTemplate.withBasicAuth(用户名,密码)

@Autowired
private TestRestTemplate testRestTemplate;

@Test
public void testOAuthAccessTokenIsReturned() {
    MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
    request.set("username", USERNAME);
    request.set("password", password);
    request.set("grant_type", "password");
    @SuppressWarnings("unchecked")
    Map<String, Object> token = this.testRestTemplate.withBasicAuth(CLIENT_NAME, CLIENT_PASSWORD)
            .postForObject(SyntheticsConstants.OAUTH_ENDPOINT, request, Map.class);
    assertNotNull("Wrong response: " + token, token.get("access_token"));
}
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法在更中心的地方做到这一点?这样我就必须在每个需要基本身份验证的测试用例中重复密码。可以使用 TestRestTemplate 以某种方式始终使用基本身份验证吗?如何将 testRestTemplate 作为 @Bean 注入? (2认同)