Spring security 为了在集成测试中绕过Springboot安全性,要模拟哪个类 @SpringBootTest(webEnvironment=RANDOM\u端口) @ActiveProfiles(“测试”) 类ContextLoadingSpec扩展了规范{ @自动连线 TestRestTemplate TestRestTemplate def“/应加载上下文“(){ 什么时候: ResponseEntity=testRestTemplate.getForEntity(“/”,Object.class) 然后: entity.statusCode==HttpStatus.OK } @测试配置 静态类配置{ @豆子 RestTemplateBuilder RestTemplateBuilder(){ 返回新的RestTemplateBuilder() .basicAuthorization('user','xxxxxxxx') } } }

Spring security 为了在集成测试中绕过Springboot安全性,要模拟哪个类 @SpringBootTest(webEnvironment=RANDOM\u端口) @ActiveProfiles(“测试”) 类ContextLoadingSpec扩展了规范{ @自动连线 TestRestTemplate TestRestTemplate def“/应加载上下文“(){ 什么时候: ResponseEntity=testRestTemplate.getForEntity(“/”,Object.class) 然后: entity.statusCode==HttpStatus.OK } @测试配置 静态类配置{ @豆子 RestTemplateBuilder RestTemplateBuilder(){ 返回新的RestTemplateBuilder() .basicAuthorization('user','xxxxxxxx') } } },spring-security,integration-testing,spock,spring-boot-test,Spring Security,Integration Testing,Spock,Spring Boot Test,在这里创建TestConfiguration对我来说似乎不正确。凭据应位于“application.yml”中,并且身份验证调用应已被模拟。请建议一种更好的测试方法 @SpringBootTest(webEnvironment = RANDOM_PORT) @ActiveProfiles("test") class ContextLoadingSpec extends Specification { @Autowired TestRestTemplate testRestTem

在这里创建TestConfiguration对我来说似乎不正确。凭据应位于“application.yml”中,并且身份验证调用应已被模拟。请建议一种更好的测试方法

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("test")
class ContextLoadingSpec extends Specification {

    @Autowired
    TestRestTemplate testRestTemplate

    def '/ should load the context'() {
        when:
        ResponseEntity<Object> entity = testRestTemplate.getForEntity('/', Object.class)

        then:
        entity.statusCode == HttpStatus.OK
    }

    @TestConfiguration
    static class Config {

        @Bean
        RestTemplateBuilder restTemplateBuilder() {
            return new RestTemplateBuilder()
            .basicAuthorization('user', 'xxxxxxxx')
        }
    }
}