- 浏览: 2551183 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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:
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:
发表评论
-
RESTful JSON Mock Server
2015-03-19 11:58 800RESTful JSON Mock Server C ... -
Performance Tool(7)Improve Lua and Wrk
2015-01-17 06:37 1036Performance Tool(7)Improve Lua ... -
Performance Tool(6)Gatling Upgrade to 2.1.2 Version Or wrk
2015-01-10 01:15 979Performance Tool(6)Gatling Upg ... -
Performance Tool(5)Upgrade to 2.0.x
2014-08-27 03:34 1127Performance Tool(5)Upgrade to 2 ... -
Performance Tool(4)CSV File Data Feeder
2014-08-25 10:50 1031Performance Tool(4)CSV File Dat ... -
wrk with LuaJIT
2014-08-19 06:30 1335wrk with LuaJITHere is an exa ... -
Performance Tool(3)Gatling Upgrade and Cluster
2014-07-25 02:32 1335Performance Tool(3)Gatling Upgr ... -
WRK a HTTP Benchmarking Tool
2014-03-07 04:42 1144WRK a HTTP Benchmarking Tool1 ... -
Performance Tool(1)Gatling
2013-03-15 05:28 1294Performance Tool(1)Gatling 1. ... -
Jenkins Configuration(4)Improve Shell Script Debug/Info Message
2013-01-07 06:32 1338Jenkins Configuration(4)Improve ... -
Jenkins Configuration(3)Shell Script
2012-12-28 01:17 2682Jenkins Configuration(3)Shell S ... -
Eclipse Plugin(2)SOAP UI
2012-06-08 10:48 1352Eclipse Plugin(2)SOAP UI Plugi ... -
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
2012-04-06 15:57 1911Spring3 and REST Integeration(V ... -
Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter
2012-04-06 15:41 2839Spring3 and REST Integration(V) ... -
Spring3 and REST Integration(IV)Controller JUnit Test and Mock/Servlet
2012-04-06 15:13 1989Spring3 and REST Integration(IV ... -
Jbehave(2)Some Improvement and POM changes
2012-03-28 23:11 1428Jbehave(2)Some Improvement and ... -
buildr(1)Introduce and Install
2011-12-23 16:37 2189buildr(1)Introduce and Install ... -
Jbehave(1) First Web Page Sample
2011-10-26 15:00 2205Jbehave(1) First Web Page Sampl ... -
WarcraftIII Problem on English Win7
2011-07-25 10:18 1946WarcraftIII Problem on English ... -
Web Performance Test Tool
2011-05-10 15:37 1458Web Performance Test Tool 1. F ...
相关推荐
1. **配置测试环境**:引入Spring Test和JUnit相关的依赖,创建一个继承自`AbstractJUnit4SpringContextTests`或`SpringRunner`的测试类。在测试类上使用`@RunWith(SpringRunner.class)`注解启用Spring测试支持,并...
struts-junit spring-mock spring-test junit等的javadoc.jar格式的API文档,直接导入Eclipse/MyEclipse/Netbeans等IDE即可实现快速API查询。 包含以下文件: File name -------------------------------------- ...
标题“spring2 junit3”指的是在Spring框架的第二个主要版本中使用JUnit3进行单元测试的相关内容。这篇博文可能探讨了如何在Spring 2.x时代利用JUnit3进行测试驱动开发(TDD),因为JUnit3是当时广泛使用的Java单元...
接下来,我们将深入探讨Spring Test与JUnit4的结合使用以及如何通过它们进行测试代码的编写。 首先,Spring Test模块提供了一组测试注解,如`@ContextConfiguration`、`@RunWith(SpringRunner.class)`等,这些注解...
分别是两个版本的, 因为spring做单元测试的时候, 是很容易出现版本不兼容的情况, 所以我将我用到的jar包分享出来,zip包中内容:hamcrest-core-1.3、junit4.4、junit-4.12、spring_test2.5.5、spring-test-3.2.0....
3. **设置JUnitTest** 首先,在Android项目中创建一个测试目录,通常位于`app/src/androidTest/java`。然后,在这个目录下创建一个新的Java类,继承自`androidx.test.ext.junit.runners.AndroidJUnit4`。这样,你的...
《Spring Framework 2.5.6与JUnit 4.4及Spring Test的深度解析》 在软件开发领域,Spring Framework以其强大的依赖注入、面向切面编程(AOP)以及全面的事务管理等功能,成为了Java应用开发的重要基石。而这次我们...
spring-test-junit5, JUnit ( a )的spring TestContext框架扩展( a ) spring 5测试支持这个项目作为 5的正式 Prototype,在 spring TestContext框架测试支持,并与 SPR-13575结合到 Spring Framework 。 因此,在...
分别是两个版本的, 因为spring做单元测试的时候, 是很容易出现版本不兼容的情况, 所以我将我用到的jar包分享出来,zip包中内容:spring_test2.5.5、spring-test-3.2.0.RELEASE 这个我忘了上传有关于junit的jar ,...
在SSM环境中,使用Junit4和spring-test库进行单元测试是标准做法。下面将详细解释如何使用这两个库以及所需的jar包。 Junit4是Java领域广泛使用的单元测试框架,它提供了一套丰富的注解,使得编写测试用例变得更加...
在Java开发过程中,单元测试是确保代码质量的重要环节,而JUnit是Java领域广泛使用的单元测试框架。Mockito则是一个强大的模拟框架,它允许我们在测试中创建和配置模拟对象,以便隔离被测试代码并专注于测试单个行为...
《Spring3与JUnit4深度解析》 在Java开发领域,Spring框架和JUnit测试工具是不可或缺的重要组成部分。Spring3.2.8是Spring框架的一个稳定版本,它提供了丰富的功能,包括依赖注入、AOP(面向切面编程)、MVC(模型-...
本示例程序是关于"Spring Boot Test"的实践,它展示了如何进行Spring Rest Controller的集成测试和单元测试。 1. **Spring Boot Test模块**: Spring Boot提供了测试支持模块,包含`spring-boot-starter-test`,这...
JUnit5引入了新的注解,如@Test、@BeforeEach、@AfterEach等,使得测试更加灵活和可定制。此外,JUnit5还支持并行测试,提高了测试执行效率。 【Mockito使用】 Mockito是一个强大的Mock框架,它可以创建Mock对象并...
这包括JUnit4本身,Spring的`spring-test`模块以及Spring Boot的`spring-boot-starter-test`模块。这三个依赖分别提供了JUnit4的基本功能,Spring对测试的支持以及Spring Boot测试相关的工具和配置。以下是添加依赖...
Spring Test与JUnit的结合,为Spring应用的测试提供了强大的组合。通过使用`@RunWith(SpringJUnit4ClassRunner.class)`,我们可以将Spring的IoC容器引入到JUnit测试中,使得Spring的组件可以直接在测试环境中被管理...
在本篇文章中,我们将探讨如何利用Spring的MOVE(Model-View-Controller)架构以及JUnit库来执行单元测试。首先,我们需要理解Spring的MOVE架构: 1. **Model**:这是应用的核心业务逻辑部分,它处理数据和业务规则...
在这个“spring3 junit 测试”主题中,我们将深入探讨如何在Spring3环境中集成和使用JUnit进行单元测试。Spring3提供了对JUnit的内置支持,允许我们在测试上下文中注入依赖,模拟服务,以及使用其强大的测试支持类。...
"SpringTest_springtest_spring_java_Framework_"这个标题暗示了我们讨论的是关于Spring框架的测试方面,可能是使用Spring进行单元测试或集成测试的一些实践。 描述中的“简单小应用,实现了一些基本的功能”可能是...
这是一个基于Spring3、MyBatis3和JUnit4的可运行项目示例,旨在提供一个完整的、可测试的Java Web应用程序框架。这个项目的核心是利用Spring作为应用的ioc(Inversion of Control,控制反转)和aop(Aspect Oriented...