英文:
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;
}
}
当测试时,adapterConfig
为null
,但在正常运行时会被设置和创建,有任何想法吗?
英文:
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 来注入完整的配置。
英文:
Using @WebMvcTest, the container will inject only components related to Spring MVC (@Controller, @ControllerAdvice, etc.) not the full configuration use @SpringBootTest with @AutoConfigureMockMvc instead.
答案2
得分: 1
Keycloak的AutoConfiguration没有被@WebMvcTest包括。
您可以:
- 通过
@Import(org.keycloak.adapters.springboot.KeycloakSpringBootConfiguration.class)
手动包含它。 - 或者使用
@SpringBootTest
。
英文:
Keycloak's AutoConfiguration is not included by @WebMvcTest
.
You could
- Include it manually via
@Import(org.keycloak.adapters.springboot.KeycloakSpringBootConfiguration.class)
- 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 ....
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论