当前位置:   article > 正文

Java单元测试_实体类的unit test

实体类的unit test

  单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Java里单元指一个类,图形化的软件中可以指一个窗口或一个菜单等。总的来说,单元就是人为规定的最小的被测功能模块。单元测试是在软件开发过程中要进行的最低级别的测试活动,软件的独立单元将在与程序的其他部分相隔离的情况下进行测试。

  可以说,单元测试(模块测试)是开发者编写的一小段代码,用于检验被测代码的一个很小的、很明确的功能是否正确。通常而言,一个单元测试是用于判断某个特定条件(或者场景)下某个特定函数的行为。可见,单元测试是由程序员自己来完成,最终受益的也是程序员自己。可以这么说,程序员有责任编写功能代码,同时也就有责任为自己的代码编写单元测试。执行单元测试,就是为了证明这段代码的行为和我们期望的一致。

  对于程序员来说,如果养成了对自己写的代码进行单元测试的习惯,不但可以写出高质量的代码,而且还能提高编程水平。

  接下来主要介绍Junit3,Junit4单元测试方案,以及mock测试框架,Spring单元测试框架。我们先来看Junit3单元测试方案,代码如下:

  1. package com.itszt.domain;
  2. import java.util.Date;
  3. /**
  4. * 单元测试用的实体类
  5. */
  6. public class Order {
  7. private int orderId;//订单order的id
  8. private Date orderTime;//下订单时间
  9. public Order(int orderId, Date orderTime) {
  10. this.orderId = orderId;
  11. this.orderTime = orderTime;
  12. }
  13. @Override
  14. public String toString() {
  15. return "Order{" +
  16. "orderId=" + orderId +
  17. ", orderTime=" + orderTime +
  18. '}';
  19. }
  20. public Order() {
  21. }
  22. public int getOrderId() {
  23. return orderId;
  24. }
  25. public void setOrderId(int orderId) {
  26. this.orderId = orderId;
  27. }
  28. public Date getOrderTime() {
  29. return orderTime;
  30. }
  31. public void setOrderTime(Date orderTime) {
  32. this.orderTime = orderTime;
  33. }
  34. }
  35. -------------------------------------------------------------
  36. package com.itszt.dao;
  37. import com.itszt.domain.Order;
  38. import java.util.*;
  39. /**
  40. * 测试单元,操作订单的类
  41. */
  42. public class OrderDao {
  43. private static int moneyNum=100;
  44. private static Map<String,List<Order>> allOrders=new HashMap<>();
  45. private static int id=1;
  46. static {
  47. List<Order> orders=new ArrayList<>();
  48. orders.add(new Order(id++,new Date()));
  49. orders.add(new Order(id++,new Date()));
  50. orders.add(new Order(id++,new Date()));
  51. allOrders.put("张三",orders);//属于张三的订单
  52. }
  53. public int queryOrderCount(String username){
  54. //用户所下订单的数量
  55. return allOrders.get(username).size();
  56. }
  57. public List<Order> queryOrders(String username){
  58. //用户下过的订单
  59. return allOrders.get(username);
  60. }
  61. public int queryMoney(){
  62. return moneyNum;
  63. }
  64. public void addMoney(int num){
  65. moneyNum=moneyNum+num;
  66. }
  67. public void dropMoney(int num){
  68. moneyNum=moneyNum-num;
  69. }
  70. }
  71. ------------------------------------------------------
  72. package test1;
  73. import com.itszt.dao.OrderDao;
  74. import com.itszt.domain.Order;
  75. import junit.framework.TestCase;
  76. import java.util.List;
  77. /**
  78. * Junit3,创建单元测试用例
  79. * 注:Junit4用注解完成
  80. */
  81. public class TestOrderDao extends TestCase{
  82. private OrderDao orderDao;
  83. private static int num=0;
  84. @Override
  85. public void setUp() throws Exception {
  86. System.out.println((++num)+"--------========================");
  87. // setUp:在每个测试方法前都会执行,做通用初始化
  88. // super.setUp();
  89. orderDao=new OrderDao();
  90. System.out.println("test1.TestOrderDao.setUp");
  91. }
  92. @Override
  93. public void tearDown() throws Exception {
  94. // super.tearDown();
  95. System.out.println("test1.TestOrderDao.tearDown");
  96. }
  97. public void testQueryMoney(){
  98. System.out.println("test1.TestOrderDao.testQueryMoney");
  99. int queryMoney = orderDao.queryMoney();
  100. System.out.println("queryMoney = " + queryMoney);
  101. //传入一个期望值,再传入一个真实值,看两者是否相等
  102. assertEquals(100,queryMoney);
  103. }
  104. public void testAddMoney(){
  105. System.out.println("test1.TestOrderDao.testAddMoney");
  106. orderDao.addMoney(100);
  107. int queryMoney = orderDao.queryMoney();
  108. System.out.println("queryMoney = " + queryMoney);
  109. assertEquals(200,queryMoney);
  110. }
  111. public void testDropMoney(){
  112. System.out.println("TestOrderDao.testDropMoney");
  113. orderDao.dropMoney(35);
  114. int queryMoney = orderDao.queryMoney();
  115. System.out.println("queryMoney = " + queryMoney);
  116. assertEquals(165,queryMoney);
  117. }
  118. public void testQueryOrderCount(){
  119. System.out.println("TestOrderDao.testQueryOrderCount");
  120. int orderCount = orderDao.queryOrderCount("张三");
  121. System.out.println("orderCount = " + orderCount);
  122. assertEquals(3,orderCount);
  123. }
  124. public void testQueryOrders(){
  125. System.out.println("TestOrderDao.testQueryOrders");
  126. List<Order> orderList = orderDao.queryOrders("张三");
  127. System.out.println("orderList = " + orderList);
  128. assertEquals(orderList,orderList);
  129. }
  130. }
  131. -------------------------------------------------------
  132. package test1;
  133. import junit.framework.Test;
  134. import junit.framework.TestCase;
  135. import junit.framework.TestSuite;
  136. /**
  137. *单元测试套件,运行测试用例
  138. */
  139. public class TestAll extends TestCase{
  140. public static Test suite(){
  141. TestSuite testSuite=new TestSuite();
  142. testSuite.addTestSuite(TestOrderDao.class);
  143. return testSuite;
  144. }
  145. }

  上述代码执行结果如下:

  1. 1--------========================
  2. test1.TestOrderDao.setUp
  3. TestOrderDao.testQueryOrders
  4. orderList = [Order{orderId=1, orderTime=Wed Mar 21 14:43:51 CST 2018}, Order{orderId=2, orderTime=Wed Mar 21 14:43:51 CST 2018}, Order{orderId=3, orderTime=Wed Mar 21 14:43:51 CST 2018}]
  5. test1.TestOrderDao.tearDown
  6. 2--------========================
  7. test1.TestOrderDao.setUp
  8. test1.TestOrderDao.testQueryMoney
  9. queryMoney = 100
  10. test1.TestOrderDao.tearDown
  11. 3--------========================
  12. test1.TestOrderDao.setUp
  13. test1.TestOrderDao.testAddMoney
  14. queryMoney = 200
  15. test1.TestOrderDao.tearDown
  16. 4--------========================
  17. test1.TestOrderDao.setUp
  18. TestOrderDao.testDropMoney
  19. queryMoney = 165
  20. test1.TestOrderDao.tearDown
  21. 5--------========================
  22. test1.TestOrderDao.setUp
  23. TestOrderDao.testQueryOrderCount
  24. orderCount = 3
  25. test1.TestOrderDao.tearDown

  总的来说,在使用Junit3单元测试时,步骤如下:

  先导入相关jar包;

  新建单元测试用例,如:public class TestUserDao extends TestCase;

  其中,setUp:在每个测试方法前都会执行,做通用初始化;

      tearDown:在每个测试方法后都会执行,做通用资源释放。

  写测试方案:其实就是一堆方法,这些方法通常以test开头即可;

  怎么判断功能是否正常:基于Assert 断言完成;

  测试结果通常是3种: 1.成功 2.失败 3.异常报错

  单元测试套件:一次执行多个测试类

  上面用的是Junit3测试方案,Junit4与Junit3不同的是,采用了注解方式,从而使得测试代码更为简洁。我们接下来看Junit4测试方案:

  1. package com.itszt.dao;
  2. /**
  3. * 测试单元
  4. */
  5. public class UserDao {
  6. private static int moneyNum=100;
  7. public int queryMoney(){
  8. System.out.println(10/0);//模拟出现异常
  9. return moneyNum;
  10. }
  11. public void addMoney(int num){
  12. moneyNum+=num;
  13. }
  14. public void dropMoney(int num){
  15. moneyNum-=num;
  16. }
  17. }
  18. ----------------------------------------------------------
  19. package test2;
  20. import com.itszt.dao.UserDao;
  21. import org.junit.After;
  22. import org.junit.Assert;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. /**
  26. * 测试用例,通过注解声明
  27. */
  28. public class TestUserDao {
  29. private UserDao userDao;
  30. @Before
  31. public void init(){
  32. userDao=new UserDao();
  33. System.out.println("TestUserDao.init");
  34. }
  35. @After
  36. public void tearDown(){
  37. System.out.println("TestUserDao.tearDown");
  38. }
  39. @Test
  40. public void testQueryMoney(){
  41. System.out.println("TestUserDao.testQueryMoney "+userDao.hashCode());
  42. int money = userDao.queryMoney();
  43. Assert.assertEquals(100,money);
  44. }
  45. @Test
  46. public void testAddMoney(){
  47. System.out.println("TestUserDao.testAddMoney "+userDao.hashCode());
  48. userDao.addMoney(300);
  49. int money = userDao.queryMoney();
  50. Assert.assertEquals(400,money);
  51. }
  52. }
  53. ----------------------------------------------------
  54. package test2;
  55. import org.junit.runner.RunWith;
  56. import org.junit.runners.Suite;
  57. /**
  58. * 测试套件
  59. * 测试用例多于一个时,中间以英文逗号分割
  60. */
  61. @Suite.SuiteClasses({TestUserDao.class})
  62. @RunWith(Suite.class)
  63. public class TestAll {
  64. }

  上述代码执行结果如下:

  1. TestUserDao.init
  2. TestUserDao.testQueryMoney 25282035
  3. TestUserDao.tearDown
  4. java.lang.ArithmeticException: / by zero
  5. at com.itszt.dao.UserDao.queryMoney(UserDao.java:9)
  6. at test2.TestUserDao.testQueryMoney(TestUserDao.java:28)
  7. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  8. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  9. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  10. at java.lang.reflect.Method.invoke(Method.java:498)
  11. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  12. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  13. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  14. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  15. at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  16. at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  17. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  18. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  19. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  20. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  21. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  22. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  23. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  24. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  25. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  26. at org.junit.runners.Suite.runChild(Suite.java:128)
  27. at org.junit.runners.Suite.runChild(Suite.java:27)
  28. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  29. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  30. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  31. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  32. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  33. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  34. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  35. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  36. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
  37. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
  38. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  39. TestUserDao.init
  40. TestUserDao.testAddMoney 4959864
  41. TestUserDao.tearDown
  42. java.lang.ArithmeticException: / by zero
  43. at com.itszt.dao.UserDao.queryMoney(UserDao.java:9)
  44. at test2.TestUserDao.testAddMoney(TestUserDao.java:35)
  45. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  46. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  47. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  48. at java.lang.reflect.Method.invoke(Method.java:498)
  49. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  50. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  51. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  52. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  53. at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  54. at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  55. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  56. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  57. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  58. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  59. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  60. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  61. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  62. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  63. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  64. at org.junit.runners.Suite.runChild(Suite.java:128)
  65. at org.junit.runners.Suite.runChild(Suite.java:27)
  66. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  67. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  68. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  69. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  70. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  71. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  72. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  73. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  74. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
  75. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
  76. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

  可见,代码出现异常后,单元测试随之会给出相应的异常提示。

  我们接下来用mock方法执行单元测试。首先,我们要在项目中添加easymock-3.4.jar包的依赖。测试代码如下:

  1. package com.itszt.domain;
  2. /**
  3. * 测试用的实体类
  4. */
  5. public class Org {
  6. private String orgName,orgLoaction,orgType;
  7. public Org(String orgName, String orgLoaction, String orgType) {
  8. this.orgName = orgName;
  9. this.orgLoaction = orgLoaction;
  10. this.orgType = orgType;
  11. }
  12. public Org() {
  13. }
  14. public String getOrgName() {
  15. return orgName;
  16. }
  17. public void setOrgName(String orgName) {
  18. this.orgName = orgName;
  19. }
  20. public String getOrgLoaction() {
  21. return orgLoaction;
  22. }
  23. public void setOrgLoaction(String orgLoaction) {
  24. this.orgLoaction = orgLoaction;
  25. }
  26. public String getOrgType() {
  27. return orgType;
  28. }
  29. public void setOrgType(String orgType) {
  30. this.orgType = orgType;
  31. }
  32. @Override
  33. public String toString() {
  34. return "Org{" +
  35. "orgName='" + orgName + '\'' +
  36. ", orgLoaction='" + orgLoaction + '\'' +
  37. ", orgType='" + orgType + '\'' +
  38. '}';
  39. }
  40. }
  41. ----------------------------------------------------
  42. package com.itszt.dao;
  43. import com.itszt.domain.Org;
  44. /**
  45. * 操作Org对象的接口
  46. */
  47. public interface OrgDao {
  48. //1.查重
  49. public Org queryOrgByName(String orgName);
  50. }
  51. --------------------------------------------------------
  52. package com.itszt.service;
  53. /**
  54. * 操作Org业务的接口
  55. */
  56. public interface OrgService {
  57. public boolean regOrg(String orgName,String orgLoaction,String orgType);
  58. }
  59. ----------------------------------------------------------
  60. package com.itszt.service;
  61. import com.itszt.dao.OrgDao;
  62. import com.itszt.domain.Org;
  63. /**
  64. * OrgService的实现类
  65. */
  66. public class OrgServiceImpl implements OrgService {
  67. private OrgDao orgDao;
  68. public void setOrgDao(OrgDao orgDao) {
  69. this.orgDao = orgDao;
  70. }
  71. @Override
  72. public boolean regOrg(String orgName, String orgLoaction, String orgType) {
  73. Org org = orgDao.queryOrgByName(orgName);
  74. if (org == null) {
  75. System.out.println(orgName+"不存在,可以执行插入动作。");
  76. } else {
  77. System.out.println(orgName+"已存在,不能再插入");
  78. return false;
  79. }
  80. return true;
  81. }
  82. }
  83. -------------------------------------------------------
  84. package test4;
  85. import com.itszt.dao.OrgDao;
  86. import com.itszt.domain.Org;
  87. import com.itszt.service.OrgServiceImpl;
  88. import org.easymock.EasyMock;
  89. import org.junit.Assert;
  90. import org.junit.Test;
  91. /**
  92. * 用mock方案实现单元测试
  93. */
  94. public class TestOrgService {
  95. @Test
  96. public void testRegOrg(){
  97. OrgServiceImpl orgService=new OrgServiceImpl();
  98. //用mock模拟一个UserDao的实现类
  99. OrgDao orgDao = EasyMock.createMock(OrgDao.class);
  100. orgService.setOrgDao(orgDao);
  101. //待测试的一个实体类
  102. Org org = new Org("曹操公司", "许昌", "魏国");
  103. //当orgDao调用queryOrgByName方法,并且传入参数为"曹操公司"时,则返回org对象,该模拟对象orgDao可使用次数为10
  104. //传入期望值,实际结果为Org对象或null
  105. EasyMock.expect(orgDao.queryOrgByName("曹操公司")).andReturn(org).times(10);
  106. EasyMock.expect(orgDao.queryOrgByName("刘备公司")).andReturn(null).times(10);
  107. //让我们模拟的特性生效
  108. EasyMock.replay(orgDao);
  109. boolean boo1 = orgService.regOrg("曹操公司", "许昌", "魏国");
  110. Assert.assertFalse(boo1);
  111. boolean boo2 = orgService.regOrg("刘备公司", "成都", "蜀汉");
  112. Assert.assertTrue(boo2);
  113. }
  114. }

   上述代码执行如下:

  1. 曹操公司已存在,不能再插入
  2. 刘备公司不存在,可以执行插入动作。

  我们接下来再写一个基于mock的单元测试方案:

  1. package com.itszt.controller;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpSession;
  4. /**
  5. * 测试单元
  6. */
  7. public class UserController {
  8. public void showUser(HttpServletRequest request, HttpSession session) {
  9. String username = request.getParameter("username");
  10. System.out.println("简单使用一下---username = " + username);
  11. String userpwd = request.getAttribute("userpwd").toString();
  12. System.out.println("简单使用一下---userpwd = " + userpwd);
  13. String userNow = session.getAttribute("userNow").toString();
  14. System.out.println("简单使用一下---userNow = " + userNow);
  15. }
  16. }
  17. -------------------------------------------------
  18. package test3;
  19. import com.itszt.controller.UserController;
  20. import org.easymock.EasyMock;
  21. import org.junit.Test;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpSession;
  24. /**
  25. * mock单元测试方案
  26. */
  27. public class TestUserController {
  28. @Test
  29. public void testShow(){
  30. UserController userController=new UserController();
  31. HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
  32. HttpSession session = EasyMock.createMock(HttpSession.class);
  33. String username="小明";
  34. String userpwd="123456";
  35. String userNow=username;
  36. EasyMock.expect(request.getParameter("username")).andReturn(username).times(1);
  37. EasyMock.expect(request.getAttribute("userpwd")).andReturn(userpwd).once();
  38. EasyMock.expect(session.getAttribute("userNow")).andReturn("正在登录的是:"+userNow).once();
  39. EasyMock.replay(request);
  40. EasyMock.replay(session);
  41. userController.showUser(request,session);
  42. }
  43. }

  上述代码执行结果如下:

  1. 简单使用一下---username = 小明
  2. 简单使用一下---userpwd = 123456
  3. 简单使用一下---userNow = 正在登录的是:小明

  最后,我们在使用Spring框架时,Spring也为我们提供了单元测试方案。在使用时,我们需要添加Spring框架中相应的jar包(spring-test-4.3.11.RELEASE.jar)依赖。

  由于我们在案例中使用了注解,所以在spring-config.xml配置文件中要添加注解支持,配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--添加注解支持-->
  10. <context:annotation-config></context:annotation-config>
  11. <context:component-scan base-package="test4"></context:component-scan>
  12. </beans>

  接下来是基于Spring框架的单元测试代码:

  1. package test4;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * 测试单元
  5. */
  6. @Component
  7. public class UserService {
  8. public int queryScore(){
  9. System.out.println("100就对了");
  10. return 100;
  11. }
  12. }
  13. ---------------------------------------------------
  14. package test4;
  15. import org.junit.After;
  16. import org.junit.Before;
  17. import org.junit.Test;
  18. import org.junit.runner.RunWith;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.test.context.ContextConfiguration;
  21. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  22. /**
  23. * 测试用例与套件
  24. */
  25. @RunWith(SpringJUnit4ClassRunner.class)
  26. @ContextConfiguration(locations = "classpath:config/spring-config.xml")
  27. public class TestUserService {
  28. @Autowired
  29. private UserService userService;
  30. @Before
  31. public void before(){
  32. System.out.println("运行前测试....");
  33. }
  34. @After
  35. public void after(){
  36. System.out.println("运行完成后测试...");
  37. }
  38. @Test
  39. public void testQueryScore(){
  40. System.out.println("TestUserService.testQueryScore");
  41. int queryScore = userService.queryScore();
  42. org.junit.Assert.assertEquals(100,100);
  43. }
  44. }

  上述代码运行如下:

  1. 运行前测试....
  2. TestUserService.testQueryScore
  3. 100就对了
  4. 运行完成后测试...
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/274047
推荐阅读
相关标签
  

闽ICP备14008679号