Spring3 and REST Integration(VI)Controller JUnit Test and Mock/Spring HandlerAda

Spring3 and REST Integration(VI)Controller JUnit Test and Mock/Spring HandlerAdapter
Spring Annotation and HandlerAdapter
There is no need to use other class, but only the configuration. And I can use mockito to mock our service/manager layer.
The pom.xml changes will be as follow:
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-mock</artifactId]]>
<version]]>2.0.8</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.mockito</groupId]]>
<artifactId]]>mockito-core</artifactId]]>
<version]]>1.9.0</version]]>
</dependency]]>

The test class implementation will be as follow:
package com.sillycat.easyrestserver.controller;

import java.io.IOException;

import javax.servlet.ServletException;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping;

import com.sillycat.easyrestserver.model.Company;
import com.sillycat.easyrestserver.model.Person;
import com.sillycat.easyrestserver.service.PersonService;

/**
* use mockito to mock our service/manager
* new Controller and use handler to invoke the controller
* @author karl
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test-context.xml")
public class PersonControllerTest3 {

@Mock
private PersonService mockPersonService;

@Autowired
HandlerAdapter handlerAdapter;

ObjectMapper jsonMapper;
Person person;
PersonController personController;
MockHttpServletRequest mockRequest;
MockHttpServletResponse mockResponse;

@Before
public void setUp() throws ServletException, IOException {
MockitoAnnotations.initMocks(this);
jsonMapper = new ObjectMapper();
person = new Person();
person.setCompany(new Company());
person.setId(3);
person.setPersonName("person3");

personController = new PersonController();
personController.setPersonService(mockPersonService);

mockRequest = new MockHttpServletRequest();
mockRequest.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING,
true);

mockResponse = new MockHttpServletResponse();
}

@Test
public void get() throws Exception {
mockRequest.setMethod("GET");
mockRequest.setRequestURI("/person/3");
mockRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
mockResponse = new MockHttpServletResponse();

Mockito.when(mockPersonService.get(3)).thenReturn(person);

handlerAdapter.handle(mockRequest, mockResponse, personController);
Assert.assertEquals(mockResponse.getStatus(), 200);
Person actualPerson = jsonMapper.readValue(
mockResponse.getContentAsString(), Person.class);
Assert.assertEquals(actualPerson.getId(), person.getId());
}

@Test
public void add() throws Exception {
mockRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
mockRequest.setMethod("POST");
mockRequest.setRequestURI("/person");

person.setId(null);
String jsonPerson = jsonMapper.writeValueAsString(person);
mockRequest.setContent(jsonPerson.getBytes());


handlerAdapter.handle(mockRequest, mockResponse, personController);

Assert.assertEquals(mockResponse.getStatus(), 200);
}

}


references:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值