繁体   English   中英

避免在 junit4 中调用 CommandLineRunner

[英]avoid call CommandLineRunner in junit4

我正在开发一个使用 spring boot 2.1.1.RELEASE和 junit 4 的项目。

这是一个命令行应用程序,它依赖于CommandLineRunner作为“main”。

问题是我需要编写一个使用一些@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 使用@Profile("!test")声明 bean CommandLineRunner并配置您的测试类以启动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 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.ExceptionTranslation85Filter@5c springframework.security.web.access.intercept.FilterSecurityInterceptor@4d174189]
2020-02-14 19:38:29.586 INFO 41437 --- [main] czscSkipCommandLineRunnerTest:在 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) 在MyTest类中包含用@Configuration注释的内部静态类,然后将用于加载测试上下文。 在任何情况下,您都需要删除@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM