【问题标题】:Spring Boot Test with @WebMvcTest fails on Atlassian Bitbucket Pipeline在 Atlassian Bitbucket 管道上使用 @WebMvcTest 进行 Spring Boot 测试失败
【发布时间】:2018-08-29 16:51:06
【问题描述】:

在本地运行一些@WebMvcTests 时,我没有问题(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 时存在的容器。为什么?

【问题讨论】:

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


    【解决方案1】:

    原来缺少一些关键 bean,因为 @WebMvc 注释不会拾取 @Components,只有 Web 堆栈需要的,不包括存储库,但问题是我的安全配置,我也想测试它我现在只是@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 部分编写更多测试。我猜这是一件好事。

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2020-11-12
      • 1970-01-01
      • 2022-01-05
      • 2017-05-28
      • 2017-09-30
      • 2017-05-05
      • 2022-10-05
      • 2021-07-26
      相关资源
      最近更新 更多