配置在测试中被忽略

huangapple go评论94阅读模式
英文:

Configuration Getting Ignored in Test

问题

我尝试测试我的Spring应用程序,但遇到以下问题:
在"正常模式"(mvn spring-boot:run)下,应用程序如预期启动,adapterConfig被设置并且不为NULL。但当我启动我的测试类来测试MVC时,adapterConfig未被设置。Spring忽略了整个配置类。

测试:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = StudentController.class)
public class StudentControllerTests {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private StudentService service;
    @MockBean
    private StudentRepository repository;
    @Test
    public void shouldReturnABC() throws Exception{
        MvcResult result = this.mockMvc.perform(get("/students/abc")).andReturn();
    }
}

控制器:

@RestController
@RequestMapping("/students")
@PermitAll
public class StudentController {
    @Autowired
    StudentService studentService;
    //get
    @GetMapping("/abc")
    public String abc (){
        return "abc";
    }
}

配置:

@Configuration
public class SpringBootKeycloakConfigResolver implements KeycloakConfigResolver {
    private KeycloakDeployment keycloakDeployment;
    private AdapterConfig adapterConfig;
    @Autowired
    public SpringBootKeycloakConfigResolver(AdapterConfig adapterConfig) {
        this.adapterConfig = adapterConfig;
    }
    @Override
    public KeycloakDeployment resolve(OIDCHttpFacade.Request request) {
        if (keycloakDeployment != null) {
            return keycloakDeployment;
        }
        keycloakDeployment = KeycloakDeploymentBuilder.build(adapterConfig);
        return keycloakDeployment;
    }
}

当测试时,adapterConfignull,但在正常运行时会被设置和创建,有任何想法吗?

英文:

I try to test my spring app but encounter following problem:
In "normal mode"(mvn spring-boot:run) the app starts as expected and adapterConfig gets set and is NOT NULL. When I start my testclass to test the MVC, adapterConfig does not get set. Spring ignores the whole config class.

test:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = StudentController.class)
public class StudentControllerTests {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private StudentService service;
    @MockBean
    private StudentRepository repository;
    @Test
    public void shouldReturnABC() throws Exception{
        MvcResult result = this.mockMvc.perform(get("/students/abc")).andReturn();
    }
}

controller:

@RestController
@RequestMapping("/students")
@PermitAll
public class StudentController {
    @Autowired
    StudentService studentService;
    //get
    @GetMapping("/abc")
    public String abc (){
        return "abc";
    }

config:

@Configuration
public class SpringBootKeycloakConfigResolver implements KeycloakConfigResolver {
    private KeycloakDeployment keycloakDeployment;
    private AdapterConfig adapterConfig;
    @Autowired
    public SpringBootKeycloakConfigResolver(AdapterConfig adapterConfig) {
        this.adapterConfig = adapterConfig;
    }
    @Override
    public KeycloakDeployment resolve(OIDCHttpFacade.Request request) {
        if (keycloakDeployment != null) {
            return keycloakDeployment;
        }
        keycloakDeployment = KeycloakDeploymentBuilder.build(adapterConfig);
        return keycloakDeployment;
    }
}

adapterConfig is null when hitting the test but gets set & created when hitting it the normal way, any idea?

答案1

得分: 2

使用 @WebMvcTest,容器将仅注入与Spring MVC相关的组件(@Controller、@ControllerAdvice等),而不是使用 @SpringBootTest 和 @AutoConfigureMockMvc 来注入完整的配置。

Spring Boot Javadoc

英文:

Using @WebMvcTest, the container will inject only components related to Spring MVC (@Controller, @ControllerAdvice, etc.) not the full configuration use @SpringBootTest with @AutoConfigureMockMvc instead.

Spring Boot Javadoc

答案2

得分: 1

Keycloak的AutoConfiguration没有被@WebMvcTest包括。

您可以:

  1. 通过@Import(org.keycloak.adapters.springboot.KeycloakSpringBootConfiguration.class)手动包含它。
  2. 或者使用@SpringBootTest
英文:

Keycloak's AutoConfiguration is not included by @WebMvcTest.

You could

  1. Include it manually via @Import(org.keycloak.adapters.springboot.KeycloakSpringBootConfiguration.class)
  2. Or use @SpringBootTest

答案3

得分: 0

在Spring Boot 2.5中,我必须在我的测试中导入 KeycloakAutoConfiguration

@WebMvcTest(value = ApplicationController.class, properties = "spring.profiles.active:test")
@Import(KeycloakAutoConfiguration.class)
public class WebLayerTest { 
        // ... 测试代码 ....
}
英文:

with spring boot 2.5 i had I had to import KeycloakAutoConfiguration into my test.

@WebMvcTest(value = ApplicationController.class, properties = "spring.profiles.active:test")
@Import(KeycloakAutoConfiguration.class)
public class WebLayerTest { 
        // ... test code ....
}

huangapple
  • 本文由 发表于 2020年8月9日 16:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63324336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定