Spring单元测试类ApplicationTests错误
来源:互联网 发布:网络维护费用清单 编辑:程序博客网 时间:2024/06/05 14:20
1)正确写法
package com.boot.demo02restful;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.boot.restful.Application;import com.boot.restful.service.UserService;@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes=Application.class)public class ApplicationTests {@Autowired@Qualifier(value="myUserService")private UserService userSerivce;@Beforepublic void setUp() {// 准备,清空user表userSerivce.deleteAllUsers();}@Testpublic void test() throws Exception {// 插入5个用户userSerivce.create("a", 1);userSerivce.create("b", 2);userSerivce.create("c", 3);userSerivce.create("d", 4);userSerivce.create("e", 5);// 查数据库,应该有5个用户Assert.assertEquals(5, userSerivce.getAllUsers().intValue());// 删除两个用户userSerivce.deleteByName("a");userSerivce.deleteByName("e");// 查数据库,应该有5个用户Assert.assertEquals(3, userSerivce.getAllUsers().intValue());}}
2)异常写法
package com.boot.demo02restful;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.boot.restful.Application;import com.boot.restful.service.UserService;@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTestpublic class ApplicationTests {@Autowired@Qualifier(value="myUserService")private UserService userSerivce;@Beforepublic void setUp() {// 准备,清空user表userSerivce.deleteAllUsers();}@Testpublic void test() throws Exception {// 插入5个用户userSerivce.create("a", 1);userSerivce.create("b", 2);userSerivce.create("c", 3);userSerivce.create("d", 4);userSerivce.create("e", 5);// 查数据库,应该有5个用户Assert.assertEquals(5, userSerivce.getAllUsers().intValue());// 删除两个用户userSerivce.deleteByName("a");userSerivce.deleteByName("e");// 查数据库,应该有5个用户Assert.assertEquals(3, userSerivce.getAllUsers().intValue());}}
阅读全文
1 0
- Spring单元测试类ApplicationTests错误
- spring+hiberante整合单元测试遇到的错误
- spring+hiberante整合单元测试遇到的错误
- [JavaEE] Spring单元测试中缺少 ServletContext 错误
- Spring单元测试
- Spring单元测试
- spring单元测试
- spring单元测试
- spring单元测试
- Spring单元测试
- spring 单元测试
- Spring单元测试
- spring单元测试
- spring 单元测试
- Spring单元测试
- spring 单元测试
- Spring单元测试
- spring 单元测试
- Java数据结构和算法——数组、单向链表、双向链表
- imagesview
- 测试 File and LinkedHashMap 示例
- 双指针
- Ajax
- Spring单元测试类ApplicationTests错误
- python_configpaser
- linux下nfs服务器的安装和配置
- 【HDU
- Oracle 和 Mysql 的分页语句
- C++程序员学Java系列之二六:数据结构之枚举(Enumeration)
- 大数据全攻略:10年老兵带你看尽MaxCompute大数据运算挑战与实践
- KNX协议入门
- Oracle 中 decode 函数用法