Question

I'm trying to test my controller mapping and the response http status codes. Therefore I'm using the RequestMappingHandlerAdapter and the RequestMappingHandlerMapping.

My Controller

@Controller
@RequestMapping(value ="/user")
public class AdminSpringController {

    @Autowired
    public UserAdminService userService;

    private final Logger log = LoggerFactory.getLogger(AdminSpringController.class);

    @RequestMapping(method = RequestMethod.GET, consumes = "application/json", produces = "application/json")
    @ResponseStatus(HttpStatus.OK)
    public List<User> getUsers() {

        log.trace("Request to get all users.");

        return userService.getUsers();
    }
}

and my Test class:

public class AdminSpringControllerTests {

    AdminSpringController cut;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private RequestMappingHandlerAdapter adapter;
    private RequestMappingHandlerMapping handlerMapping; 

    @Test
    public void testGetSc() throws Exception{

        adapter = new RequestMappingHandlerAdapter();
        handlerMapping = new RequestMappingHandlerMapping();

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setMethod("GET");
        request.setRequestURI("/user");
        request.addHeader("Accept", "application/json");

        MockHttpServletResponse response = new MockHttpServletResponse();

        Object handler = handlerMapping.getHandler(request).getHandler();

        ModelAndView mav = adapter.handle(request, response, handler);

        assertNotNull(mav);
    }
}

But I'm getting an NullPointerException in the row Object handler = ...

I simply want to test my mapping and then check the HTTP status codes from my response. Is this the right approach or is my test implementation totally wrong. For the tests I need to use EasyMock. As next step I wanted to test if the response status codes (response.getStatus()) is equals to SC_OK (200).

Thx for any help :)

Was it helpful?

Solution

I am using SpringMvc Test approach, mixed with EasyMock, which is a good approach.

@ContextConfiguration("ProjectFacadeTest-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class AdminSpringControllerTests {
...
private MockMvc mockMvc;
...
@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders.standaloneSetup(imageController).build();
}

EasyMock.expect(mockPersistedProjectService.getImages(EasyMock.anyLong())).andReturn(images);
EasyMock.replay(mockPersistedProjectService);
    MvcResult result =
            this.mockMvc.perform(get("/resources/projects/1000/images")).andExpect(content().type("application/json"))
                    .andExpect(status().isOk()).andReturn();
MockHttpServletResponse response = result.getResponse();
//VERIFY RESPONSE
EasyMock.verify(mockPersistedProjectService);

XML FILE

<bean id="mockArtifactService" class="org.easymock.EasyMock"
    factory-method="createStrictMock" primary="true">
  <constructor-arg value="com.xxxxxx.service.ArtifactService" />
</bean>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top