簡體   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