瀏覽代碼

新增 spring mvc 单元测试

jobob 9 年之前
父節點
當前提交
18983c3322

+ 19 - 6
mybatis-plus/pom.xml

@@ -43,6 +43,7 @@
 		<mybatis-ehcache.version>1.0.3</mybatis-ehcache.version>
 		<junit.version>4.12</junit.version>
 		<contiperf.version>2.3.4</contiperf.version>
+		<mockito.version>2.1.0-RC.1</mockito.version>
 	</properties>
 
 	<dependencies>
@@ -113,15 +114,27 @@
 			<scope>test</scope>
 		</dependency>
 		<dependency>
-		    <groupId>junit</groupId>
-		    <artifactId>junit</artifactId>
-		    <version>${junit.version}</version>
+			<groupId>org.springframework</groupId>
+			<artifactId>spring-test</artifactId>
+			<version>${spring.version}</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>${junit.version}</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.databene</groupId>
+			<artifactId>contiperf</artifactId>
+			<version>${contiperf.version}</version>
 			<scope>test</scope>
 		</dependency>
 		<dependency>
-		    <groupId>org.databene</groupId>
-		    <artifactId>contiperf</artifactId>
-		    <version>${contiperf.version}</version>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-core</artifactId>
+			<version>${mockito.version}</version>
 			<scope>test</scope>
 		</dependency>
 		<!-- test end -->

+ 38 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/framework/RestResponse.java

@@ -0,0 +1,38 @@
+package com.baomidou.mybatisplus.test.framework;
+
+public class RestResponse<T> {
+
+	private boolean success;
+	private String message;
+	private T data;
+
+	public RestResponse(boolean success, String message, T data) {
+		this.success = success;
+		this.message = message;
+		this.data = data;
+	}
+
+	public boolean getSuccess() {
+		return success;
+	}
+
+	public void setSuccess(boolean success) {
+		this.success = success;
+	}
+
+	public String getMessage() {
+		return message;
+	}
+
+	public void setMessage(String message) {
+		this.message = message;
+	}
+
+	public T getData() {
+		return data;
+	}
+
+	public void setData(T data) {
+		this.data = data;
+	}
+}

+ 43 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/framework/UserController.java

@@ -0,0 +1,43 @@
+package com.baomidou.mybatisplus.test.framework;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import com.baomidou.mybatisplus.test.framework.service.IUserService;
+
+@Controller
+@RequestMapping("/user")
+public class UserController {
+
+	@Autowired
+	private IUserService userService;
+
+//	@ResponseBody
+//	@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
+//	public RestResponse<User> getUser(@PathVariable("id") int id) {
+//		User user = userService.getUser(id);
+//		return new RestResponse<User>(true, "", user);
+//	}
+//
+//	@ResponseBody
+//	@RequestMapping(method = RequestMethod.POST, produces = "application/json")
+//	public RestResponse saveUser(@RequestBody User user) {
+//		userService.saveUser(user);
+//		return new RestResponse<String>(true, "", null);
+//	}
+//
+//	@ResponseBody
+//	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "application/json")
+//	public RestResponse deleteUser(@PathVariable("id") int id) {
+//		userService.deleteUser(id);
+//		return new RestResponse<String>(true, "", null);
+//	}
+//
+//	@ResponseBody
+//	@RequestMapping(method = RequestMethod.PUT, produces = "application/json")
+//	public RestResponse updateUser(@RequestBody User user) {
+//		userService.updateUser(user);
+//		return new RestResponse<String>(true, "", null);
+//	}
+}

+ 89 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/framework/UserControllerTest.java

@@ -0,0 +1,89 @@
+package com.baomidou.mybatisplus.test.framework;
+
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import com.baomidou.mybatisplus.test.framework.service.IUserService;
+
+/*
+ * Retrieve user with ID=3: GET http://localhost:8080/user/3
+Save a new User: POST http://localhost:8080/user
+Update User: PUT http://localhost:8080/user
+Delete use with ID=3: DELETE http://localhost:8080/user/3
+
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({ "classpath:spring-test-context.xml", "classpath:mockTestBeans.xml" })
+public class UserControllerTest {
+
+	@InjectMocks
+	private UserController userController;
+
+	private MockMvc mockMvc;
+
+	@Spy
+	private IUserService userService;
+
+	@Before
+	public void setup() {
+		// this must be called for the @Mock annotations above to be processed
+		// and for the mock service to be injected into the controller under
+		// test.
+		MockitoAnnotations.initMocks(this);
+		mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
+	}
+
+	// @Test
+	// public void testGet() throws Exception {
+	//
+	// int userId = 3;
+	//
+	// mockMvc.perform(
+	// get("/user/" + userId))
+	// .andExpect(MockMvcResultMatchers.status().isOk())
+	// .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+	// .andExpect(jsonPath("data.age", is(15)))
+	// .andExpect(jsonPath("data.firstName", is("bob")))
+	// .andExpect(jsonPath("data.id", is(userId)))
+	// .andExpect(jsonPath("success", is(true)));
+	// }
+	//
+	// @Test
+	// public void testSave() throws Exception {
+	// mockMvc.perform(
+	// post("/user")
+	// .contentType(TestUtil.APPLICATION_JSON_UTF8)
+	// .content(TestUtil.convertObjectToJsonBytes(new User()))
+	// )
+	// .andExpect(MockMvcResultMatchers.status().isOk())
+	// .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+	// .andExpect(jsonPath("success", is(true)));
+	// }
+	//
+	// @Test
+	// public void testUpdate() throws Exception {
+	// mockMvc.perform(
+	// put("/user")
+	// .contentType(TestUtil.APPLICATION_JSON_UTF8)
+	// .content(TestUtil.convertObjectToJsonBytes(new User())))
+	// .andExpect(MockMvcResultMatchers.status().isOk())
+	// .andExpect(jsonPath("success", is(true)));
+	// }
+	//
+	// @Test
+	// public void testDelete() throws Exception {
+	// mockMvc.perform(
+	// delete("/user/3"))
+	// .andExpect(MockMvcResultMatchers.status().isOk())
+	// .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+	// .andExpect(jsonPath("success", is(true)));
+	// }
+
+}

+ 8 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/framework/service/IUserService.java

@@ -0,0 +1,8 @@
+package com.baomidou.mybatisplus.test.framework.service;
+
+import com.baomidou.framework.service.ISuperService;
+import com.baomidou.mybatisplus.test.mysql.entity.User;
+
+public interface IUserService extends ISuperService<User> {
+	
+}

+ 12 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/framework/service/UserServiceImpl.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.framework.service;
+
+import org.springframework.stereotype.Service;
+
+import com.baomidou.framework.service.impl.SuperServiceImpl;
+import com.baomidou.mybatisplus.test.mysql.UserMapper;
+import com.baomidou.mybatisplus.test.mysql.entity.User;
+
+@Service
+public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements IUserService {
+	
+}

+ 15 - 0
mybatis-plus/src/test/resources/spring-test-context.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans:beans xmlns="http://www.springframework.org/schema/mvc"
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+             xmlns:beans="http://www.springframework.org/schema/beans"
+             xmlns:context="http://www.springframework.org/schema/context"
+             xmlns:mvc="http://www.springframework.org/schema/task"
+             xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
+		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
+
+    <mvc:annotation-driven/>
+
+    <context:component-scan base-package="com.baomidou.mybatisplus.test"/>
+
+</beans:beans>