首先須要jar包java
http://mvnrepository.com/artifact/junit/junit/4.12web
在IDEA使用的話 把原有的功能進行測試spring
//判讀字符串第一個不重複的字符 @Override public char getStr(String str) { //indexOf();lastIndexOf(); if (str == null || str.length() == 0 || str.equals("") || str.trim().isEmpty()) { return 0; } Map<Character, Integer> counts = new LinkedHashMap<Character, Integer>( str.length()); for (char c : str.toCharArray()) { counts.put(c, counts.containsKey(c) ? counts.get(c) + 1 : 1); } for (Map.Entry<Character, Integer> entry : counts.entrySet()) { if (entry.getValue() == 1) { return entry.getKey(); } } return 0; } //隨機字符串 @Override public String getRandStr(int length) { //定義一個字符串(A-Z,a-z,0-9)即62位; String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; //由Random生成隨機數 Random random = new Random(); StringBuilder sb = new StringBuilder(); //長度爲幾就循環幾回 for (int i = 0; i < length; ++i) { //產生0-61的數字 int number = random.nextInt(62); //將產生的數字經過length次承載到sb中 sb.append(str.charAt(number)); } //將承載的字符轉換成字符串 return sb.toString(); }
如何使用Junit進行測試呢?express
快捷鍵Ctrl + Shift + Tapache
選擇createapi
購選所需方法就能夠了mybatis
須要選擇Junitmvc
便可運行測試app
綠色說明經過dom
須要測試的方法
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()); }}