使用@WebMvcTest的Spring Boot Test在Atlassian Bitbucket Pipeline上失败

时间:2018-03-20 18:21:21

标签: java spring spring-boot bitbucket-pipelines spring-mvc-test

在本地运行一些@WebMvcTest时,我没有问题(Spring Boot 1.5.8,gradle 4.6):

@RunWith(SpringRunner.class)
@WebMvcTest(VZNFCController.class)
public class VZNFCControllerTest {

  private VZNFCTagAction action1 = new VZNFCTagAction();
  private VZNFCTagAction action2 = new VZNFCTagAction();

  @Before
  public void setUp(){
      action1.setMessage("message1");
      action2.setMessage("message2");
  }


  @Autowired
  private MockMvc mvc;

  @MockBean
  private VZNFCTagActionRepository actionRepository;

  @MockBean
  private MappingMongoConverter mongoConverter;

  @Test
  @WithMockUser
  public void testGetTagList() throws Exception {

    given(this.actionRepository.findAll())
              .willReturn(Arrays.asList(action1, action2));  
    this.mvc.perform(get("/api/nfc/tags")
            .accept(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(status().isOk());
    }
}

然而,当我上传到Atlassian Bitbucket并在那里运行./gradlew test --stacktrace时,我得到以下内容:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.

现在Bibucket管道使用Docker Image(java:8)。当我在本地切换回@SpringBoot@AutoConfigureMockMvc时,我在两种环境中都会遇到相同的错误。两个环境中的相同数据库设置(MongoDB的相同docker镜像),相同的一切......使用Docker时,是否有些端口无法映射?我想我确实创建了Servlet请求......

修改

在Docker容器中模拟Bitbucket管道构建(如建议here),似乎嘲笑MappingMongoConverter并与@SpringBootTest一起移动到@AutoConfigureMockMvc就足够了让它运行。因此@WebMvcTest只有部分模拟的上下文就足够了,没有容器,但它会在Docker容器中失败,例如使用Bitbucket时存在的容器。为什么呢?

1 个答案:

答案 0 :(得分:0)

原来有一些关键bean缺失,因为@WebMvc注释不会提取@Components,只有网页堆栈不需要包含存储库所需的内容,但问题是我的我想测试的安全配置,我现在只是@Import(以及控制器所依赖的其他一些bean):

@RunWith(SpringRunner.class)
@WebMvcTest(value = VZNFCController.class)
@Import(value = {VZApiSecurityConfiguration.class, 
                 VZJwtTokenUtils.class, VZProperties.class})
public class VZNFCControllerTest {

  @Autowired
  private MockMvc mvc;

  @MockBean
  private VZNFCTagService tagService;

  /* same as the above... I don't mock out the repository any more */

}

这里学到的经验是,所有自动配置的测试上下文(@WebMvc@DataMongoTest等)都会加快测试速度(这很好,因为我在bitbucket上支付了构建时间)。但是你需要真正了解运行应用程序所需的内容。它迫使我真的只是模拟服务专注于控制器,然后为我的应用程序的DAO部分编写更多测试。我猜这是件好事。