[英]How to load beans into test context?
I have the REST service that I need to testing.我有需要测试的 REST 服务。 That service has a spring security authentication and I need to turn off it in my tests or mock.
该服务具有 spring 安全身份验证,我需要在我的测试或模拟中关闭它。 I decided to mock it because I couldn't turn off it.
我决定嘲笑它,因为我无法关闭它。 I write
@TestConfiguration
for that but now my context not load:我
@TestConfiguration
写了@TestConfiguration
,但现在我的上下文没有加载:
@TestConfiguration
public class TestConfig {
}
@WebMvcTest(controller = MyController.class)
@ContextConfiguration(classes = TestConfig.class)
public MyControllerTest {
@Test
public void simpleTest() {
}
}
and in my main source, I have some config class that load some other beans and it class did not load in my test and I have an exception:在我的主要源代码中,我有一些加载其他 bean 的配置类,但它的类没有在我的测试中加载,我有一个例外:
java.lang.IllegalStateException: Failed to load ApplicationContext
What I do wrong?我做错了什么? Can anyone help me?
谁能帮我? I using
SpringBoot 2.2.0
and in that version @WebMvcTest(secure = false)
not working because of secure
property doesn't exist anymore.我使用
SpringBoot 2.2.0
并且在那个版本中@WebMvcTest(secure = false)
由于secure
属性不再存在而无法工作。
You could try overriding the security configuration in your test class:您可以尝试覆盖测试类中的安全配置:
@WebMvcTest(controller = MyController.class)
public MyControllerTest {
@Configuration
public class MyTestsSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//...
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//...
}
}
@Test
public void simpleTest() {
}
}
Also have a look at this here: https://howtodoinjava.com/spring-boot2/testing/springboot-test-configuration/也看看这里: https : //howtodoinjava.com/spring-boot2/testing/springboot-test-configuration/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.