Skip to content

Commit

Permalink
添加hunit和mock和mockMvc测试
Browse files Browse the repository at this point in the history
  • Loading branch information
2010yhh committed Mar 12, 2019
1 parent de1701f commit 62767b4
Show file tree
Hide file tree
Showing 11 changed files with 418 additions and 107 deletions.
25 changes: 25 additions & 0 deletions springboot-druid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down Expand Up @@ -56,6 +61,26 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* @Description:
* @Description:测试durid连接池
* @Author: yanhonghai
* @Date: 2018/9/17 1:00
*/
Expand All @@ -28,19 +27,62 @@ public class UserController {
@Autowired
UserService userService;


@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Object addUser(@RequestBody User user) {
Map<String, Object> result = new HashMap<>();
try {
userService.addUser(user);
result.put("code", "200");
result.put("msg", "success");
} catch (Exception e) {
result.put("code", "201");
result.put("msg", ExceptionUtils.getFullStackTrace(e));
}
return result;
}
@RequestMapping(method = RequestMethod.DELETE)
@ResponseBody
public Object deleteUser(@RequestParam int id) {
Map<String, Object> result = new HashMap<>();
try {
userService.deleteUser(id);
result.put("code", "200");
result.put("msg", "success");
} catch (Exception e) {
result.put("code", "201");
result.put("msg", ExceptionUtils.getFullStackTrace(e));
}
return result;
}
/**
* http://localhost:8090/user/testDb
* http://localhost:8090/user?userName=admin
* @return
*/
@RequestMapping(value = {"/testDb"})
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public Object getUser(@RequestParam String userName) {
Map<String, Object> result = new HashMap<>();
try {
User user = userService.findByUserName(userName);
result.put("code", "200");
result.put("msg", "success");
result.put("result", user);
} catch (Exception e) {
result.put("code", "201");
result.put("msg", ExceptionUtils.getFullStackTrace(e));
}
return result;
}
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public Object testDb() {
public Object updateUser(@RequestBody User user) {
Map<String, Object> result = new HashMap<>();
try {
List<User> users = userService.findAll();
userService.updateUser(user);
result.put("code", "200");
result.put("msg", "success");
result.put("result", users);
} catch (Exception e) {
result.put("code", "201");
result.put("msg", ExceptionUtils.getFullStackTrace(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
public interface UserService {
public User findByUserName(String userName);
public List<User> findAll();
public void addUser(User user);
public void deleteUser(int id);
public void updateUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.ctg.test.durid.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
Expand All @@ -19,6 +20,7 @@
* @Date: 2018/9/17 0:54
*/
@Service

public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
Expand All @@ -36,4 +38,22 @@ public User findByUserName(String userName) {
public List<User> findAll() {
return userMapper.selectByExample(new UserExample());
}

@Override
@Transactional
public void addUser(User user) {
userMapper.insertSelective(user);
}

@Override
@Transactional
public void deleteUser(int id) {
userMapper.deleteByPrimaryKey(id);
}

@Override
@Transactional
public void updateUser(User user) {
userMapper.updateByPrimaryKeySelective(user);
}
}
1 change: 0 additions & 1 deletion springboot-druid/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
server.port=8090
#server.context-path=/test-shiro
##tomcat##
server.tomcat.uri-encoding=UTF-8
##Druid##
Expand Down
111 changes: 13 additions & 98 deletions springboot-druid/src/main/resources/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,111 +10,26 @@ Target Server Type : MYSQL
Target Server Version : 80000
File Encoding : 65001
Date: 2018-11-19 18:01:09
Date: 2019-03-12 10:29:47
*/

SET FOREIGN_KEY_CHECKS=0;

------------------
-- Table structure for sys_permission
-- ----------------------------
DROP TABLE IF EXISTS `sys_permission`;
CREATE TABLE `sys_permission` (
`perm_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '权限id',
`perm_name` varchar(20) NOT NULL COMMENT '权限名',
PRIMARY KEY (`perm_id`),
UNIQUE KEY `perm_name` (`perm_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='权限表';

-- ----------------------------
-- Records of sys_permission
-- ----------------------------
INSERT INTO `sys_permission` VALUES ('1', 'add');
INSERT INTO `sys_permission` VALUES ('2', 'delete');
INSERT INTO `sys_permission` VALUES ('4', 'query');
INSERT INTO `sys_permission` VALUES ('3', 'update');

-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`role_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色id',
`role_name` varchar(255) NOT NULL COMMENT '角色名',
PRIMARY KEY (`role_id`),
UNIQUE KEY `role_name` (`role_name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='角色表';

-- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES ('1', 'admin');
INSERT INTO `sys_role` VALUES ('2', 'role1');
INSERT INTO `sys_role` VALUES ('3', 'role2');

-- ----------------------------
-- Table structure for sys_role_permission
-- Table structure for cas_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_permission`;
CREATE TABLE `sys_role_permission` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色-权限关联表的id',
`role_id` int(11) NOT NULL COMMENT '角色id',
`perm_id` int(11) NOT NULL COMMENT '权限id',
DROP TABLE IF EXISTS `cas_user`;
CREATE TABLE `cas_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(50) DEFAULT NULL,
`pass_word` varchar(21000) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `perm_id` (`perm_id`) USING BTREE,
KEY `role_id` (`role_id`) USING BTREE,
CONSTRAINT `sys_role_permission_perm_id` FOREIGN KEY (`perm_id`) REFERENCES `sys_permission` (`perm_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `sys_role_permission_role_id` FOREIGN KEY (`role_id`) REFERENCES `sys_role` (`role_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='角色-权限表';
UNIQUE KEY `user_name` (`user_name`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

-- ----------------------------
-- Records of sys_role_permission
-- Records of cas_user
-- ----------------------------
INSERT INTO `sys_role_permission` VALUES ('1', '1', '1');
INSERT INTO `sys_role_permission` VALUES ('2', '1', '2');
INSERT INTO `sys_role_permission` VALUES ('3', '1', '3');
INSERT INTO `sys_role_permission` VALUES ('4', '1', '4');
INSERT INTO `sys_role_permission` VALUES ('5', '2', '4');
INSERT INTO `sys_role_permission` VALUES ('6', '3', '4');

-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`user_id` int(40) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`user_name` varchar(255) NOT NULL COMMENT '用户名',
`real_name` varchar(255) DEFAULT NULL COMMENT '真实名',
`pass_word` varchar(255) NOT NULL COMMENT '用户密码',
`telephone` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`photo` varchar(255) DEFAULT NULL,
`create_time` datetime DEFAULT CURRENT_TIMESTAMP,
`update_time` datetime DEFAULT CURRENT_TIMESTAMP,
`status` int(10) DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=242 DEFAULT CHARSET=utf8 COMMENT='用户表';

-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES ('238', 'admin', 'user104_RealName', '038bdaf98f2037b31f1e75b5b4c9b26e', '1111111', null, '111', '2018-10-09 18:50:03', '2018-09-28 19:28:06', null);
INSERT INTO `sys_user` VALUES ('239', 'user1', 'user1_RealName', 'd6e75e048d00e4d0378d904bfdf81870', null, null, null, '2018-10-23 11:35:55', '2018-10-23 11:35:55', null);
INSERT INTO `sys_user` VALUES ('240', 'user2', 'user2_RealName', '2fec513e98ba7033049be21d92f55fa3', null, null, null, '2018-10-23 14:29:51', '2018-10-23 14:29:51', null);
INSERT INTO `sys_user` VALUES ('241', 'yhh', 'yhh', 'e22fdab996d9aca5be5ba6ae4f361255', null, null, null, '2018-11-13 22:37:08', '2018-11-13 22:37:08', null);

-- ----------------------------
-- Table structure for sys_user_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户-角色关联表id',
`user_id` int(40) NOT NULL COMMENT '用户id',
`role_id` int(11) NOT NULL COMMENT '角色id',
PRIMARY KEY (`id`),
KEY `role_id` (`role_id`) USING BTREE,
KEY `user_id` (`user_id`) USING BTREE,
CONSTRAINT `sys_user_role_role_id` FOREIGN KEY (`role_id`) REFERENCES `sys_role` (`role_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `sys_user_role_user_id` FOREIGN KEY (`user_id`) REFERENCES `sys_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='用户-角色表';
INSERT INTO `cas_user` VALUES ('1', 'admin', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `cas_user` VALUES ('2', 'user1', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `cas_user` VALUES ('3', 'user2', 'e10adc3949ba59abbe56e057f20f883e');
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.ctg.test.durid;

/**
* @Description: junit4单元测试
* @Author: yanhonghai
* @Date: 2019/3/12 9:30
*/
import org.junit.*;

public class Junit4TestCase {

@BeforeClass
public static void setUpBeforeClass() {
System.out.println("Set up before class");
}

@Before
public void setUp() throws Exception {
System.out.println("Set up");
}

@Test
public void testMathPow() {
System.out.println("Test Math.pow");
Assert.assertEquals(4.0, Math.pow(2.0, 2.0), 0.0);
}

@Test
public void testMathMin() {
System.out.println("Test Math.min");
Assert.assertEquals(2.0, Math.min(2.0, 4.0), 0.0);
}

// 期望此方法抛出NullPointerException异常
@Test(expected = NullPointerException.class)
public void testException() {
System.out.println("Test exception");
Object obj = null;
obj.toString();
}

// 忽略此测试方法
@Ignore
@Test
public void testMathMax() {
Assert.fail("没有实现");
}
// 使用“假设”来忽略测试方法
@Test
public void testAssume(){
System.out.println("Test assume");
// 当假设失败时,则会停止运行,但这并不会意味测试方法失败。
Assume.assumeTrue(false);
Assert.fail("没有实现");
}

@After
public void tearDown() throws Exception {
System.out.println("Tear down");
}

@AfterClass
public static void tearDownAfterClass() {
System.out.println("Tear down After class");
}

}
Loading

0 comments on commit 62767b4

Please sign in to comment.