废话不多说了,看一下需要几步就能搞定MVC Controller与Service层的集成测试。(如果你现在也正好使用Spring Test框架,可以看看下面对TestNG基类封装的代码,我觉得自己写得还可以。小小赞美一下啦~)
1. 定义底层Service接口及实现
/** * User service. */ public interface UserService { /** * Gets user info for specified user ID. * * @param id user ID * @return */ User getUserInfo(long id); /** * Updates user info. * * @param user user info * @return -1 means fail, 0 means success. */ int updateUserInfo(User user); }
/** * User query service. */ public interface UserQueryService { /** * Gets user name for specified user ID. * * @param userId user ID * @return */ String getUserName(long userId); /** * Updates user name for specified user ID. * * @param userId * @param userName * @return -1 means fail, 0 means success. */ int updateUserName(long userId, String userName); }
/** * User query service implementation. */ @Service public class UserQueryServiceImpl implements UserQueryService { @Autowired private UserService userService; @Override public String getUserName(long userId) { User user = this.userService.getUserInfo(userId); return user != null ? user.getName() : ""; } @Override public int updateUserName(long userId, String userName) { User user = new User(userId, userName); int udpateResult = this.userService.updateUserInfo(user); return udpateResult; } }
2. 为 Controller 层的每一接口定义一对 Request与Response(可重用的,就别多定义啦!~\(≧▽≦)/~)
/** * Base request info. * * @author Bert Lee * @version 2014-8-19 */ @JsonIgnoreProperties(ignoreUnknown = true) // 忽略多传的参数 public class BaseRequest { }
/** * User ID request info. * * @author Bert Lee * @version 2014-8-19 */ public class UserIDRequest extends BaseRequest { @JsonProperty("id") @NotNull(message = "id param is null") @Min(value = 1, message = "id param must be great or equal than \\{{value}\\}") // 4.3. Message interpolation -《JSR 303: Bean Validation》 protected long id; public long getId() { return id; } public void setId(long id) { this.id = id; } @Override public String toString() { return "UserIDRequest [id=" + id + "]"; } }
/** * User name response info. */ public class UserNameResponse { @JsonProperty("name") protected String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "UserNameResponse [name=" + name + "]"; } }
/** * User info request info. */ public class UserInfoRequest extends UserIDRequest { @JsonProperty("name") @NotNull(message = "name param is null") @Size(min = 1, message = "name param is empty") protected String userName; // 变量名与请求参数名不一样,在@RequestBody中用到 public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "UserInfoRequest [userName=" + userName + ", id=" + id + "]"; } }
/** * User modify result response info. */ public class UserResultResponse { @JsonProperty("ret") protected int result; @JsonProperty("ret_msg") protected String resultMessage; public UserResultResponse() { this.result = 0; this.resultMessage = "ok"; } public int getResult() { return result; } public void setResult(int result) { this.result = result; } public String getResultMessage() { return resultMessage; } public void setResultMessage(String resultMessage) { this.resultMessage = resultMessage; } @Override public String toString() { return "UserResultResponse [result=" + result + ", resultMessage=" + resultMessage + "]"; } }
3. 实现 Controller 层逻辑
/** * User Controller. * * @author Bert Lee * @version 2014-8-19 */ @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserQueryService userQueryService; @RequestMapping(value = "/get_user_name", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public UserNameResponse getUserName(@Valid UserIDRequest userIDRequest) { long userId = userIDRequest.getId(); String userName = this.userQueryService.getUserName(userId); UserNameResponse response = new UserNameResponse(); if (!StringUtils.isEmpty(userName)) { response.setName(userName); } return response; } @RequestMapping(value = "/update_user_name", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public UserResultResponse updateUserName(@Valid @RequestBody UserInfoRequest userInfoRequest) { // JSON request body map UserResultResponse response = new UserResultResponse(); long userId = userInfoRequest.getId(); String userName = userInfoRequest.getUserName(); int result = this.userQueryService.updateUserName(userId, userName); if (result < 0) { response.setResult(result); response.setResultMessage("update operation is fail"); } return response; } }
4. 实现一个Service与Controller层的抽象测试基类(用于集成TestNG与MVC Test框架,且自动加载配置文件)
/** * Abstract base test class for TestNG. * * @author Bert Lee * @version 2014-8-19 */ @ContextConfiguration("classpath:META-INF/spring/test-context.xml") // 集成应用上下文并加载默认的beans XML配置 public abstract class AbstractTestNGTest extends AbstractTestNGSpringContextTests { // 集成TestNG /** * Initializes the test context. */ @BeforeSuite(alwaysRun = true) public void init() { // MockitoAnnotations.initMocks(this); // 基于Spring自动装配注解,这里不再需要初始化 } }
/** * Abstract controller tier base test class for TestNG. * * @author Bert Lee * @version 2014-8-19 */ @WebAppConfiguration("src/test/java") // 集成Web应用上下文 public abstract class AbstractControllerTestNGTest extends AbstractTestNGTest { /** * MVC mock */ protected MockMvc mockMvc; /** * Gets the tested controller. * * @return the controller that is tested */ protected abstract Object getController(); /** * Setups the tested controller in MVC Mock environment. */ @BeforeClass(alwaysRun = true) public void setup() { this.mockMvc = MockMvcBuilders.standaloneSetup(this.getController()).build(); } /** * Mocks the GET request. * * @param url * @param params * @param expectedContent * @throws Exception */ protected void getMock(String url, Object[] params, String expectedContent) throws Exception { // 2. 构造GET请求 MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders .get(url, params); this.jsonRequestMock(requestBuilder, expectedContent); } /** * Mocks the POST request. * * @param url * @param paramsJson * @param expectedContent * @throws Exception */ protected void postMock(String url, String paramsJson, String expectedContent) throws Exception { // 2. 构造POST请求 MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders .post(url) .content(paramsJson) // 设置request body请求体,服务于"@RequestBody" ; this.jsonRequestMock(requestBuilder, expectedContent); } /** * Mocks the request for "application/json;charset=UTF-8" media type (Content-Type). * * @param requestBuilder * @param expectedContent * @throws Exception */ private void jsonRequestMock(MockHttpServletRequestBuilder requestBuilder, String expectedContent) throws Exception { // 2. 设置HTTP请求属性 requestBuilder.contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .characterEncoding(CharEncoding.UTF_8) ; // 3. 定义期望响应行为 this.mockMvc.perform(requestBuilder) .andDo(print()) // 打印整个请求与响应细节 .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string(expectedContent)) // 校验是否是期望的结果 ; } }
5. 实现Controller与Service层的测试逻辑
/** * Test for {@link UserController}. * * @author Bert Lee * @version 2014-8-19 */ public class UserControllerTest extends AbstractControllerTestNGTest { // tested controller @Autowired private UserController userControllerTest; // mocked service (被依赖的服务) @Autowired private UserQueryService userQueryService; @Test(dataProvider = "getUserName") public void getUserName(Object[] params, String userName, String expectedContent) throws Exception { // 1. 定义"被依赖的服务"的方法行为 when(this.userQueryService.getUserName(anyLong())).thenReturn(userName); this.getMock("/user/get_user_name?id={id}", params, expectedContent); } @DataProvider(name = "getUserName") protected static final Object[][] getUserNameTestData() { Object[][] testData = new Object[][] { { new Object[] { "23" }, "Bert Lee", "{\"name\":\"Bert Lee\"}" }, }; return testData; } @Test(dataProvider = "updateUserName") public void updateUserName(String paramsJson, Integer result, String expectedContent) throws Exception { // 1. 定义"被依赖的服务"的方法行为 when(this.userQueryService.updateUserName(anyLong(), anyString())).thenReturn(result); this.postMock("/user/update_user_name", paramsJson, expectedContent); } @DataProvider(name = "updateUserName") protected static final Object[][] updateUserNameTestData() { Object[][] testData = new Object[][] { { "{\"id\":23,\"name\":\"Bert Lee\"}", 0, "{\"ret\":0,\"ret_msg\":\"ok\"}" }, }; return testData; } @Override public Object getController() { return this.userControllerTest; } }
/** * Test for {@link UserQueryService}. * * @author Bert Lee * @version 2014-7-25 */ public class UserQueryServiceTest extends AbstractTestNGTest { // tested service @Autowired private UserQueryService userQueryServiceTest; // mocked service (被依赖的服务) @Autowired private UserService userService; @Test(dataProvider = "getUserName") public void getUserName(User user, String expected) { // 1. 定义"被依赖的服务"的方法行为 when(userService.getUserInfo(anyLong())).thenReturn(user); String userName = this.userQueryServiceTest.getUserName(3L); assertEquals(userName, expected); } @DataProvider(name = "getUserName") protected static final Object[][] getUserNameTestData() { Object[][] testData = new Object[][] { { null, "" }, { new User(3L, ""), "" }, { new User(10L, "Edward Lee"), "Edward Lee" }, { new User(23L, "李华刚@!~#$%^&"), "李华刚@!~#$%^&" }, }; return testData; } }
6. 定义XML bean配置文件,实现测试对象及被依赖的服务的自动注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd "> <bean id="userControllerTest" class="com.weibo.web.UserController" /> <!-- 被依赖的服务 --> <bean id="userQueryService" class="org.mockito.Mockito" factory-method="mock"> <constructor-arg value="com.weibo.service.UserQueryService" /> </bean> <bean id="userQueryServiceTest" class="com.weibo.service.impl.UserQueryServiceImpl" /> <!-- 被依赖的服务 --> <bean id="userService" class="org.mockito.Mockito" factory-method="mock"> <constructor-arg value="com.weibo.service.UserService" /> </bean> </beans>
7. 运行测试用例,OK!
7步搞定,挺简单的吧,O(∩_∩)O哈哈~
玩得开心!^_^
相关推荐
10. **单元测试和集成测试**:使用JUnit或TestNG进行单元测试,验证每个独立的代码单元是否正常工作;使用Mockito等工具模拟依赖关系,进行集成测试,确保整个系统的正确性。 这个项目是一个完整的Web开发实例,...
- Spring Test模块提供了测试Spring应用的支持,包括单元测试和集成测试。 - 测试可以基于JUnit、TestNG,并结合Mockito等工具进行。 10. **Spring Batch** - Spring Batch是一个用于处理大量数据的模块,适合...
7. **测试支持**:Spring 2.5包含丰富的测试工具和API,支持单元测试和集成测试,如Mockito和Spring TestNG支持,方便进行代码验证和应用测试。 8. **国际化(i18n)与本地化(l10n)**:Spring 2.5提供了对国际化...
9. **测试支持**:Spring提供了一个强大的测试框架,包括单元测试、集成测试和端到端测试工具,如Mockito和Spring TestNG支持。 10. **Java 8和更高版本的支持**:Spring 5全面兼容Java 8及以上的特性,如Lambda...
- **持续集成与测试**:Spring Test模块支持Junit和Mockito,方便进行单元测试和集成测试。 7. **性能优化** - **缓存支持**:Spring支持多种缓存机制,如 EhCache 和 Redis,提升应用性能。 - **Asynchronous ...
9. **单元测试与集成测试**:项目可能使用JUnit、Mockito等工具进行单元测试,Spring Boot Test、TestNG等进行集成测试,确保代码质量。 综上所述,这个"java项目,(spring+mybaits+springmvc)"是一个综合性的Web...
最后,Spring还支持单元测试和集成测试,提供了TestNG和JUnit支持,以及Mockito等工具进行模拟对象的创建,有助于确保代码质量。 在学习这份思维导图时,建议按照以下步骤进行: 1. 阅读并理解Spring的基本概念和...
5. **测试**:Spring提供了测试框架,支持单元测试和集成测试,包括Mockito和Spring TestNG的支持。5.2.13.RELEASE版本可能改进了测试工具和API,使得测试更加方便。 6. **模块化**:Spring框架由多个模块组成,如...
6. **测试支持**:Spring的测试模块提供了模拟对象、依赖注入以及对JUnit、TestNG等测试框架的集成,使得单元测试和集成测试变得更加简单。通过源码,我们可以了解测试环境的搭建以及如何利用`Mockito`进行模拟对象...
10. **Spring Test**:提供了对Spring应用的单元测试和集成测试支持,包括Mockito、JUnit和TestNG的集成。 11. **Spring Expression Language (SpEL)**:Spring的表达式语言,用于在运行时查询和操纵对象图,常用于...
JUnit和Mockito等工具可以辅助进行单元测试,而Struts2提供的TestNG插件则有利于进行集成测试。 9. **版本控制与构建工具**:项目可能使用Git进行版本控制,确保团队协作的效率。构建工具如Maven或Gradle则可以帮助...
- Spring Test:单元测试与集成测试 - Mockito与Spring Test Mocking - Spring Boot测试支持:JUnit、TestNG、Spock 10. **Spring实战** - 创建一个完整的Spring MVC项目 - 使用Spring Boot构建微服务应用 - ...
而Spring Boot Test或者TestNG可能用于进行集成测试,检查系统组件间的协同工作。 综上所述,SSM二手信息交易系统是一个涵盖了多种Java技术的综合项目,涉及到后端开发、前端展现、数据库操作以及测试等多个方面,...
通过Spring Boot Test、TestNG等进行集成测试,确保系统组件间的协同工作。 8. **安全控制**:系统可能采用Spring Security或Apache Shiro等框架进行权限控制,实现用户的登录、认证和授权功能,保护敏感数据的安全...
Spring的IoC(控制反转)容器管理着项目中的bean,通过@Autowired注解可以自动装配依赖,降低了组件间的耦合度。此外,Spring MVC可能用于处理HTTP请求,实现Web功能。 3. **Hibernate ORM**: 为了与数据库进行...