[英]avoid call CommandLineRunner in junit4
I'm working on a project using spring boot 2.1.1.RELEASE
with junit 4.我正在开发一个使用 spring boot
2.1.1.RELEASE
和 junit 4 的项目。
This is a command line application, that relies on a CommandLineRunner
as "main".这是一个命令行应用程序,它依赖于
CommandLineRunner
作为“main”。
The problem is I need to write a unit test that use some @Autowired
stuff问题是我需要编写一个使用一些
@Autowired
东西的单元测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ExcludeCommandLineRunner.class)
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = CommandLineRunner.class))
@ContextConfiguration(classes = ExcludeCommandLineRunner.class)
public class MyTest {
@Autowired
MyService myService;
@Test
public void foo() {
assertEquals(3, this.myService.sum(1, 2));
}
}
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = CommandLineRunner.class))
@EnableAutoConfiguration
public class ExcludeCommandLineRunner {
}
but there is no way for me to avoid the fact that the CommandLineRunner
is called... how can I do it?但是我无法避免调用
CommandLineRunner
的事实......我该怎么做?
Depending on how you configured your project, you can rely on Profile
to skip your CommandLineRunner
.根据您配置项目的方式,您可以依靠
Profile
跳过CommandLineRunner
。 Declare a bean CommandLineRunner
with a @Profile("!test")
and configure your test class to start the test
profile.使用
@Profile("!test")
声明 bean CommandLineRunner
并配置您的测试类以启动test
配置文件。
Here is a sample that works:这是一个有效的示例:
@SpringBootApplication
public class SkipCommandLineRunner {
public static void main(String[] args) {
System.setProperty("spring.config.name", "skipcommandlinerunner");
SpringApplication.run(SkipCommandLineRunner.class);
}
@Bean
@Profile("!test")
public CommandLineRunner commandLineRunner() {
return args -> {
System.out.println("I am being called");
};
}
}
@SpringBootTest
@ActiveProfiles("test")
class SkipCommandLineRunnerTest {
@Test
void test() {
System.out.println("Test is here");
}
}
2020-02-14 19:38:29.525 INFO 41437 --- [ main] ossweb.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@30e143ff, org.springframework.security.web.context.SecurityContextPersistenceFilter@5b59c3d, org.springframework.security.web.header.HeaderWriterFilter@7fd2a67a, org.springframework.security.web.csrf.CsrfFilter@779b4f9c, org.springframework.security.web.authentication.logout.LogoutFilter@484302ee, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@f0c1ae1, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@252d8df6, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@452ec287, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@410f53b2, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@46188a89, org.springframework.security.web.servletap
2020-02-14 19:38:29.525 INFO 41437 --- [main] ossweb.DefaultSecurityFilterChain:创建过滤器链:任何请求,[org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@30e143ff,org. springframework.security.web.context.SecurityContextPersistenceFilter@5b59c3d, org.springframework.security.web.header.HeaderWriterFilter@7fd2a67a, org.springframework.security.web.csrf.CsrfFilter@779b4f9c, org.springframework.security.web.authentic logout.LogoutFilter@484302ee, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@f0c1ae1, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@252d8df6, org.springframework.security.web.Default@f0c1ae1 452ec287,org.springframework.security.web.authentication.www.BasicAuthenticationFilter@410f53b2,org.springframework.security.web.savedrequest.RequestCacheAwareFilter@46188a89,org.springframework.security.web.servletap i.SecurityContextHolderAwareRequestFilter@37fca349, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@41404aa2, org.springframework.security.web.session.SessionManagementFilter@3c3cd13a, org.springframework.security.web.access.ExceptionTranslationFilter@5cb8580, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4d174189]
i.SecurityContextHolderAwareRequestFilter@37fca349,org.springframework.security.web.authentication.AnonymousAuthenticationFilter@41404aa2,org.springframework.security.web.session.SessionManagementFilter@3c3cd13a,org.springframework.security.web.access.ExceptionTranslation85Filter@5c springframework.security.web.access.intercept.FilterSecurityInterceptor@4d174189]
2020-02-14 19:38:29.586 INFO 41437 --- [ main] czscSkipCommandLineRunnerTest : Started SkipCommandLineRunnerTest in 3.22 seconds (JVM running for 4.231)2020-02-14 19:38:29.586 INFO 41437 --- [main] czscSkipCommandLineRunnerTest:在 3.22 秒内启动 SkipCommandLineRunnerTest(JVM 运行 4.231)
Test is here
测试在这里
You don't see the other I am being called
, which shows that the CommandLineRunner is excluded.您没有看到另一个
I am being called
,这表明 CommandLineRunner 被排除在外。
Hope it helps希望能帮助到你
Within @ContextConfiguration
you defined your test context configuration to be loaded from ExcludeCommandLineRunner
by Spring TestContext, therefore it will be executed.在
@ContextConfiguration
您定义了要由 Spring TestContext 从ExcludeCommandLineRunner
加载的测试上下文配置,因此它将被执行。
@ContextConfiguration(classes = ExcludeCommandLineRunner.class)
Also @SpringBootTest
annotation will search for a main configuration class (one with @SpringBootApplication (because it is in turn meta-annotated with @SpringBootConfiguration)) and use that to start a Spring application context.此外,
@SpringBootTest
注解将搜索一个主要的配置类(一个带有 @SpringBootApplication 的类(因为它反过来用 @SpringBootConfiguration 元注解))并使用它来启动一个 Spring 应用程序上下文。 In your example, you explicitly defined which class to use for application context bootstrap.在您的示例中,您明确定义了用于应用程序上下文引导的类。
@SpringBootTest(classes = ExcludeCommandLineRunner.class)
You should use one of the above annotations.您应该使用上述注释之一。
Solution : a) Specify other class(es) in @ContextConfiguration
or b) include inner static class annotated with @Configuration
within MyTest
class, which then will be used to load test context.解决方案:a) 在
@ContextConfiguration
指定其他类或 b) 在MyTest
类中包含用@Configuration
注释的内部静态类,然后将用于加载测试上下文。 In any case you need to remove @SpringBootTest
annotation.在任何情况下,您都需要删除
@SpringBootTest
注释。
@RunWith(SpringRunner.class)
public class MyTest {
@Autowired
MyService myService;
@Test
public void foo() {
assertEquals(3, this.myService.sum(1, 2));
}
@Configuration
public static class TestContextConfiguration {
// define beans (for example MyService) here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.