我正在使用带有junit 4的spring boot 2.1.1.RELEASE
开发一个项目。
这是一个命令行应用程序,它依赖于CommandLineRunner
作为“主”。
问题是我需要编写使用一些@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 {
}
但是我无法避免调用CommandLineRunner
的事实...我该怎么办?
取决于您配置项目的方式,您可以依靠Profile
跳过您的CommandLineRunner
。用CommandLineRunner
声明一个豆@Profile("!test")
并配置测试类以启动test
概要文件。
这里是一个有效的示例:
@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信息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 .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.se curity.web.servletapi.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]2020-02-14 19:38:29.586信息41437 --- [main] c.z.s.c.SkipCommandLineRunnerTest:在3.22秒内启动SkipCommandLineRunnerTest(JVM运行4.231)
测试在这里
您看不到另一个I am being called
,这表明已将CommandLineRunner排除在外。
希望有帮助
在@ContextConfiguration
内,您定义了要由Spring TestContext从ExcludeCommandLineRunner
加载的测试上下文配置,因此将执行它。
@ContextConfiguration(classes = ExcludeCommandLineRunner.class)
也@SpringBootTest
批注将搜索一个主要的配置类(一个@SpringBootApplication的类(因为它又被@SpringBootConfiguration进行元注释)),然后使用该类来启动Spring应用程序上下文。在您的示例中,您明确定义了用于应用程序上下文引导的类。
@SpringBootTest(classes = ExcludeCommandLineRunner.class)
您应使用上述注释之一。
解决方案:a)在@ContextConfiguration
中指定其他类,或b)在@Configuration
类中包括以MyTest
注释的内部静态类,然后将其用于加载测试上下文。无论如何,您都需要删除@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
}
}