簡體   English   中英

使用 Maven 運行 JUnit 測試時,Spring MockMvc WebApplicationContext 為 null

[英]Spring MockMvc WebApplicationContext is null when running JUnit Tests with Maven

我有一個包含兩個測試的 Spring mock-mvc JUnit 測試類。 當我在 Eclipse IDE 中運行測試時,兩個測試都通過了(我使用 Eclipse Maven 插件)。

從命令行運行測試時使用

mvn test

其中一項測試失敗,因為 @Autowired 的 WebApplicationContext有時為空。

這是我的測試課

@WebAppConfiguration
@ActiveProfiles({ "dev", "test" })
public class AddLinkEndpointMvcTest extends BaseMvc {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void before() {
        System.out.println("WAC = " + wac);
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void addLinkDoesNotSupportGet() throws Exception {
        mockMvc.perform(get("/myurl")).andExpect(status().is(HttpStatus.SC_METHOD_NOT_ALLOWED));
    }

    @Test
    public void addLinkBadRequestNoLinkAddress() throws Exception {
        mockMvc.perform(post("/myurl")).andExpect(status().is(HttpStatus.SC_BAD_REQUEST));
    }
}

這是 BaseMvc 類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class BaseMvc {

    @Configuration
    @ComponentScan(basePackages = { "com.example.a", "com.example.b" })
    @Profile("test")
    public static class TestConfig {

        static {
            System.out.println("TEST CONFIG");
        }

        // ...some beans defined here

        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            configurer.setIgnoreUnresolvablePlaceholders(false);
            configurer.setLocations(new Resource[] { new ClassPathResource("sample-webapp.properties"),
                new ClassPathResource("sample-domain.properties") });
            return configurer;
        }

    }

}

我已經添加了 println 調用來幫助調試。 在這里運行 mvn test 是相關的控制台輸出:

WAC = null
WAC = org.springframework.web.context.support.GenericWebApplicationContext@38795184: startup date [Thu Sep 19 16:24:22 BST 2013]; root of context hierarchy

和錯誤

java.lang.IllegalArgumentException: WebApplicationContext is required
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.<init>(DefaultMockMvcBuilder.java:66)
        at org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup(MockMvcBuilders.java:46)
        at com.mypackage.AddLinkEndpointMvcTest.before(AddLinkEndpointMvcTest.java:31)

所以這是測試中的問題行

@Autowired
private WebApplicationContext wac;  

有時 wac 為空或在 JUnit @Before 啟動之前尚未完成初始化。

我不明白的是 WebApplicationContext 有時為 null 以及為什么它會在 Eclipse IDE 中傳遞!

您應該在測試類之前添加@SpringBootTest作為注釋。 像這樣:

@RunWith(SpringRunner.class)
@SpringBootTest

請使用@WebAppConfiguration 注解。 您的問題已在此處得到解答: WebApplicationContext 不會自動裝配

嘗試使用 getBean 而不是自動連接。 這應該確保WebApplicationContext在您訪問它時被初始化。

例子:

MyClass myClass = applicationContext.getBean("myClass");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM