mercredi 29 avril 2015

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

This is very funny. When I ran my controller test with more than one tests I am getting the following error when i run it with maven, but works fine in 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) Then I ran one test commenting the other alternately (commented testA then run testB, then commented testB then run testA) both are passing. I have no idea what is happening when I put both of then are active tests. if any of you have clue please let me know. I have put my classes here.

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);*/
        if(mockMvc == null) {
        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);
        }

}

Aucun commentaire:

Enregistrer un commentaire