当多个测试添加到休息控制器测试时,为什么我需要 WebApplicationContext?

When more than one tests added to rest controller test why am I getting WebApplicationContext is required?

这很有趣。当我 运行 我的控制器用多个测试进行测试时,当我 运行 使用 Maven 时出现以下错误,但在 eclipse Junit 中工作正常。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:43) at org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup(MockMvcBuilders.java:46) at com.akrilist.rest.web.akripost.controller.AbstractRestControllerTest.setup(AbstractRestControllerTest.java:32) at com.akrilist.rest.web.akripost.controller.AutoPostControllerTest.setup(AutoPostControllerTest.java:36) 然后我 运行 一个测试交替评论另一个(评论 testA 然后 运行 testB,然后评论 testB 然后 运行 testA)都通过了。我不知道当我把两个都放在主动测试时会发生什么。如果你们有任何线索,请告诉我。我已经把我的 类 放在这里了。

AbstractRestControllerTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestRestServiceConfig.class, WebAppConfig.class })
@WebAppConfiguration
public abstract class AbstractRestControllerTest {
 protected MockMvc mockMvc;

    @Autowired
    protected WebApplicationContext webApplicationContext;

    /*@Inject
    protected UserAccountService userAccountServiceMock;*/

    @Before
    public void setup() {
       /* Mockito.reset(userAccountServiceMock);*/
     
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
     
    }
}

AutoPostControllerTest

public class AutoPostControllerTest extends AbstractRestControllerTest {

 @Autowired
 private AutoPostService autoPostServiceMock;
 @Autowired
 private AutoPostConverter autoPostConverterMock;

 @Before
 public void setup() {

  // Mockito.reset(autoPostServiceMock);
  // Mockito.reset(commentPostRepositoryMock);

  super.setup();
 }

 @Test
 public void testValidationErrorForNullProfileId() throws Exception {
  String description = TestUtil.createStringWithLength(501);
  AutoPost autoPost = new TestAutoPostBuilder().description(description).buildModel();
  mockMvc.perform(post("/auto/post").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(autoPost))).andExpect(status().isBadRequest())
    .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
    // .andExpect(jsonPath("$[]", hasSize(1)))
    .andExpect(jsonPath("$.type", is("validation failure")));

  verifyZeroInteractions(autoPostServiceMock);
 }

 @Test
 public void testGet_shouldReturnPost() throws Exception {
  String description = TestUtil.createStringWithLength(501);
  String postId = TestUtil.createStringWithLength(16);
  Integer profileId = 123456;
  TestAutoPostBuilder testAutoPostBuilder = new TestAutoPostBuilder();
  AutoPost post = testAutoPostBuilder.postId(postId).description(description).profileId(profileId).buildModel();

  when(autoPostServiceMock.get(postId)).thenReturn(post);
  when(autoPostConverterMock.convertTo(post)).thenReturn(testAutoPostBuilder.buildDto());
  mockMvc.perform(get("/auto/post/" + postId).contentType(TestUtil.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
    .andExpect(jsonPath("$.postId", is(postId))).andExpect(jsonPath("$.profileId", is(profileId))).andExpect(jsonPath("$.links", hasSize(1)));

  verify(autoPostServiceMock, times(1)).get(anyString());
  verifyNoMoreInteractions(autoPostServiceMock);
 }

}

我解决了这个问题。这是因为 maven-surefire-plugin 的并行配置。我把它的值改成了'classes',所以这个问题就结束了。我们有两种方法可以解决这个问题。一个是

<parallel>classes</parallel>
<threadCount>10</threadCount>

用需要顺序执行的 @net.jcip.annotations.NotThreadSafe 注释测试 class 的其他方式。