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

时间:2015-04-29 20:28:53

标签: rest maven spring-mvc testing controller

这很有趣。当我使用多个测试运行我的控制器测试时,我在使用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

&#13;
&#13;
@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();
    	
    }
}
&#13;
&#13;
&#13;

AutoPostControllerTest

&#13;
&#13;
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);
	}

}
&#13;
&#13;
&#13;

0 个答案:

没有答案