Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing dependencies hasSize() and hasProperty() when implementing unit test of Spring controller

I am trying to implement a unit test of a method in a Spring MVC controller as follows:

@Test
public void testGetProfile() {
    Person mockPerson = new Person();
    mockPerson.setPersonId(1);
    mockPerson.setName("Mr Brown");
    mockPerson.setAddress("Somewhere");
    mockPerson.setTelephone("1234567890"); 
    mockPerson.setEmail("[email protected]");

    when(mockPersonService.get(1)).thenReturn(mockPerson);
    try {
        mockMvc.perform(get("/person/profile?personId=1"))
            .andExpect(status().isOk())
            .andExpect(view().name("view/profile"))
            .andExpect(forwardedUrl("/WEB-INF/jsp/view/profile.jsp"))
            .andExpect(model().attribute("person", hasSize(1L)))
            .andExpect(model().attribute("person", hasItem(
                    allOf(
                            hasProperty("personId", is(1L)),
                            hasProperty("name", is("Mr Brown")),
                            hasProperty("address", is("Somewhere")),
                            hasProperty("telephone", is("1234567890")),
                            hasProperty("email", is("[email protected]")),
                    )
            )));

    }
    catch(Exception e) {
        Misc.printStackTrace(e);
    }
    verify(mockPersonService, times(1)).get(1);
    verifyNoMoreInteractions(mockPersonService);        
}

But I get messages about dependencies concerning hasSize(long) and hasProperty(...).

I have recent versions of Mockito, HamCrest et al in the application's class path.

So what am I missing?

My current imports are:

import library.model.Person;
import library.service.PersonService;
import library.util.Misc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; 
import static org.hamcrest.CoreMatchers.*;
import static org.junit.matchers.JUnitMatchers.hasItem;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
like image 510
Mr Morgan Avatar asked Jul 05 '14 15:07

Mr Morgan


1 Answers

Your imports only include the matchers from hamcrest-core, which are in CoreMatchers:

import static org.hamcrest.CoreMatchers.*;

The hasProperty and hasSize methods are only in the Matchers class of hamcrest-library, which includes a larger set of matchers. Try to change your import to the following:

import static org.hamcrest.Matchers.*;

If this does not work, you might only be depending on hamcrest-core. In this case, change your dependency to the hamcrest-library or hamcrest-all artifact. An example for adding this dependency for Maven is below. See the Hamcrest GitHub page for some more details.

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
</dependency>
like image 108
andersschuller Avatar answered Oct 07 '22 07:10

andersschuller