如何使用Junit进行测试呢?

发表于:2019-1-24 14:22

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:疯了的小蜗    来源:博客园

  快捷键Ctrl + Shift + T
  选择create
  购选所需方法就可以了
  需要选择Junit
  即可运行测试
  绿色说明通过
  需要测试的方法
 
   package Test;
  public class StringUtilImpl implements StringUtil {
  //Rot13加密解密
  @Override
  public String getRot13(String str) {
  if (str == null || str.length() == 0 || str.equals("") || str.trim().isEmpty()) {
  return null;
  } else {
  char[] chr = str.toCharArray();
  StringBuffer buffer =new StringBuffer();
  for (int i = 0; i < chr.length; i++) {
  if ((chr[i] >= 'A') && (chr[i] <= 'Z')) {
  chr[i] += 13;
  if (chr[i] > 'Z') {
  chr[i] -= 26;
  }
  } else if ((chr[i] >= 'a') && (chr[i] <= 'z')) {
  chr[i] += 13;
  if (chr[i] > 'z') {
  chr[i] -= 26;
  }
  }else if((chr[i] >= '0') && (chr[i] <= '9')){
  chr[i] +=5;
  if(chr[i] > '9'){
  chr[i] -= 10;
  }
  }
  // and return it to sender
  buffer.append(chr[i]);
  }
  return buffer.toString();
  }
  }
  //字符串翻转
  @Override
  public String getReverse(String str) {
  if (str == null || str.length()==0 || str.equals("") || str.trim().isEmpty()) {
  return null;
  }
  char[] arr = str.toCharArray();
  String reverse = "";
  for (int i = arr.length - 1; i >= 0; i--) {
  reverse += arr[i];
  }
  return reverse;
  }
  //判读字符串是否为空
  @Override
  public boolean getIsEmpty(String str) {
  if (str == null || str.length()==0 || "".equals(str) || str.trim().isEmpty()) {
  return true;
  }
  return false;
  }
  }
   测试类
   package DemoJunit;
  import Test.StringUtilImpl;
  import org.junit.Assert;
  import org.junit.Ignore;
  import org.junit.Test;
  import Test.*;
  import static org.junit.Assert.*;
  public class StringUtilImplTest {
  StringUtil s = new StringUtilImpl();
  @Test
  //@Ignore("该客户没有给钱不给他密码加密所以给他挖个坑...")
  public void getRot13() {
  Assert.assertEquals(s.getRot13("abc大Das是的3"), "nop大Qnf是的8");
  Assert.assertEquals(s.getRot13("ynxsfy"), "lakfsl");
  Assert.assertEquals(s.getRot13("I'm from guangdong"), "V'z sebz thnatqbat");
  Assert.assertEquals(s.getRot13("StringUtil"), "FgevatHgvy");
  Assert.assertEquals(s.getRot13("  "), null);
  Assert.assertEquals(s.getRot13(null), null);
  }
  @Test
  public void getReverse() {
  Assert.assertEquals(s.getReverse("are you ok?"), "?ko uoy era");
  Assert.assertEquals(s.getReverse("I me here"), "ereh em I");
  Assert.assertEquals(s.getReverse(""), null);
  Assert.assertEquals(s.getReverse(null), null);
  }
  @Test
  public void getIsEmpty() {
  String str = "";
  Assert.assertEquals(s.getIsEmpty(null), true);
  Assert.assertEquals(s.getIsEmpty("   "), true);
  Assert.assertEquals(s.getIsEmpty(""), true);
  Assert.assertEquals(s.getIsEmpty(str), true);
  Assert.assertEquals(s.getIsEmpty("GG"), false);
  }
  }
   下面来介绍 Maven项目SSM结合Junit
  这里无需添加架包在使用Maven项目字段配置好Junit
  在项目的src根目录下面创建一个Test文件夹
  然后点击右键选择
  然后文件夹就会变绿色了
  使用:
  在service选择需要测试的方法然后会自动在Test文件下面生成测试类;
  代码:
   package com.gdnf.ssm.service;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.log4j.spi.LoggerFactory;
  import org.junit.*;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import  org.apache.log4j.*;
  import java.util.logging.Logger;
  import static org.junit.Assert.*;
  public class BookServiceImplTest {
  static ClassPathXmlApplicationContext context;
  static Log logger ;
  @BeforeClass
  public static void init() {
  context = new ClassPathXmlApplicationContext("spring_root.xml");
  logger = LogFactory.getLog("");
  }
  @AfterClass
  public static void after() {
  logger.info("结束了");
  }
  //@Ignore
  @Test
  public void getCount() {
  BookService bean = context.getBean(BookService.class);
  logger.info(bean.getCount());
  logger.info(bean.getBookAll());
  }
  }
   结果
  这里配置的Log4j是输出到控制台
 "C:\Program Files\Java\jdk1.8.0_181\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\二学年\工具包\idea文件\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=64358:D:\二学年\工具包\idea文件\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\二学年\工具包\idea文件\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar;D:\二学年\工具包\idea文件\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit-rt.jar;D:\二学年\工具包\idea文件\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\二学年\学习项目\my_ssm\target\test-classes;D:\二学年\学习项目\my_ssm\target\classes;C:\Users\DZ\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\DZ\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-web\5.1.0.RELEASE\spring-web-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-beans\5.1.0.RELEASE\spring-beans-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-core\5.1.0.RELEASE\spring-core-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-jcl\5.1.0.RELEASE\spring-jcl-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-aop\5.1.0.RELEASE\spring-aop-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-jdbc\5.1.0.RELEASE\spring-jdbc-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-tx\5.1.0.RELEASE\spring-tx-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-webmvc\5.1.0.RELEASE\spring-webmvc-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-context\5.1.0.RELEASE\spring-context-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\springframework\spring-expression\5.1.0.RELEASE\spring-expression-5.1.0.RELEASE.jar;C:\Users\DZ\.m2\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;C:\Users\DZ\.m2\repository\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;C:\Users\DZ\.m2\repository\org\mariadb\jdbc\mariadb-java-client\2.3.0\mariadb-java-client-2.3.0.jar;C:\Users\DZ\.m2\repository\com\mchange\c3p0\0.9.5.2\c3p0-0.9.5.2.jar;C:\Users\DZ\.m2\repository\com\mchange\mchange-commons-java\0.2.11\mchange-commons-java-0.2.11.jar;C:\Users\DZ\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\DZ\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.7\jackson-databind-2.9.7.jar;C:\Users\DZ\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\DZ\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.7\jackson-core-2.9.7.jar;C:\Users\DZ\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.gdnf.ssm.service.BookServiceImplTest
  DEBUG [main] - ==>  Preparing: select count(name) from book
  DEBUG [main] - ==> Parameters:
  DEBUG [main] - <==      Total: 1
  九月 30, 2018 2:23:59 下午 com.gdnf.ssm.service.BookServiceImplTest getCount
  信息: 10
  DEBUG [main] - ==>  Preparing: select * from book
  DEBUG [main] - ==> Parameters:
  DEBUG [main] - <==      Total: 10
  九月 30, 2018 2:23:59 下午 com.gdnf.ssm.service.BookServiceImplTest getCount
  信息: [Book{id=1, name='干法', cnt=25}, Book{id=2, name='Java程序设计', cnt=8}, Book{id=7, name='哲学', cnt=0}, Book{id=8, name='小说', cnt=13}, Book{id=9, name='啊哈哈', cnt=13}, Book{id=10, name='cc', cnt=11}, Book{id=11, name='卓悦', cnt=57}, Book{id=12, name='他们最幸福', cnt=1}, Book{id=13, name='admin', cnt=123456789}, Book{id=14, name='我不', cnt=6}]
  九月 30, 2018 2:23:59 下午 com.gdnf.ssm.service.BookServiceImplTest after
  信息: 结束了
  Process finished with exit code 0
    package com.gdnf.ssm.service;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.log4j.spi.LoggerFactory;
  import org.junit.*;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import  org.apache.log4j.*;
  import java.util.logging.Logger;
  import static org.junit.Assert.*;
  public class BookServiceImplTest {
  static ClassPathXmlApplicationContext context;
  static Log logger ;
  @BeforeClass
  public static void init() {
  context = new ClassPathXmlApplicationContext("spring_root.xml");
  logger = LogFactory.getLog("");
  }
  @AfterClass
  public static void after() {
  logger.info("结束了");
  }
  //@Ignore
  @Test
  public void getCount() {
  BookService bean = context.getBean(BookService.class);
  logger.info(bean.getCount());
  logger.info(bean.getBookAll());
  }
  }

     上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号