摘要:步驟三為測(cè)試類聲明一個(gè)帶有參數(shù)的公共構(gòu)造函數(shù),并在其中為第二個(gè)環(huán)節(jié)中聲明的幾個(gè)變量賦值。步驟五編寫測(cè)試方法,使用定義的變量作為參數(shù)進(jìn)行測(cè)試。
What is JUnit
JUnit is a Regression Testing Framework used by developers to implement unit testing in Java and accelerate programming speed and increase the quality of code
features: Fixtures:is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.
Test suites:Test suite means bundle a few unit test cases and run it together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.
Test runners:Test runner is used for executing the test cases. Here is an example which assumes TestJunit test class already exists
JUnit classes:JUnit classes are important classes which is used in writing and testing JUnits. Some of the important classes are:
Assert which contain a set of assert methods.
TestCase which contain a test case defines the fixture to run
multiple tests.
TestResult which contain methods to collect the results of executing
a test case.
// 測(cè)試case寫法1: // 繼承TestCase ,測(cè)試方法 public void testXXX(){} 形式 import org.junit.Assert; import junit.framework.TestCase; public class TestJunit1 extends TestCase{ public void testAdd(){ String str = "JUnit is working fine"; Assert.assertEquals("JUnit is working fine", str); } } // 測(cè)試case寫法2 // 使用@Test 注解 import org.junit.Assert; import org.junit.Test; public class TestJunit1{ @Test public void testAdd(){ String str = "JUnit is working fine"; Assert.assertEquals("JUnit is working fine", str); } }
// JUnitCore 這里作為門面類用來執(zhí)行測(cè)試用例 // JUnitCore is a facade for running tests. It supports running JUnit 4 tests, // JUnit 3.8.x tests, and mixtures. To run tests from the command line, run java // org.junit.runner.JUnitCore TestClass1 TestClass2 .... For one-shot test runs, use the // static method runClasses(Class []). If you want to add special listeners, create an // instance of org.junit.runner.JUnitCore first and use it to run the tests. import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestJunit.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }愛上簡(jiǎn)單注解
// 測(cè)試類 import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; public class JunitAnnotation { //Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class. @BeforeClass public static void beforeClass(){ System.out.println("in before class"); } //This will perform the method after all tests have finished. This can be used to perform clean-up activities. @AfterClass public static void afterClass(){ System.out.println("in after class"); } //Several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before each Test method. @Before public void before(){ System.out.println("in before"); } //If you allocate external resources in a Before method you need to release them after the test runs. //Annotating a public void method with @After causes that method to be run after the Test method. @After public void after(){ System.out.println("in after"); } //The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. @Test public void test(){ System.out.println("in test"); } //The Ignore annotation is used to ignore the test and that test will not be executed. @Ignore public void ignoreTest(){ System.out.println("in ignore test"); } }
我們還是用JUnitCore來運(yùn)行這個(gè)測(cè)試: 控制臺(tái)輸出: in before class in before in test in after in after class豪華套裝(TestSuite)
先上來兩個(gè)測(cè)試用例 測(cè)試用例1 import org.junit.Assert; import junit.framework.TestCase; public class TestJunit1 extends TestCase{ public void testAdd(){ String str = "JUnit is working fine"; Assert.assertEquals("JUnit is working fine", str); } }
測(cè)試用例2 import org.junit.Test; import static org.junit.Assert.*; public class TestJunit2 { @Test public void testAdd() { //test data int num= 5; String temp= null; String str= "Junit is working fine"; //check for equality assertEquals("Junit is working fine2", str); //check for false condition assertFalse(num > 6); //check for not null value assertNotNull(str); } }
關(guān)鍵在于TestSuite的寫法 方式一: import junit.framework.*; public class JunitTestSuite { public static void main(String[] a) { // add the test"s in the suite TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class, TestJunit3.class); TestResult result = new TestResult(); suite.run(result); System.out.println("Number of test cases = " + result.runCount()); } } 直接就可以跑來了
方式二(注解形式): import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ TestJunit1.class, TestJunit2.class }) // JUnit suite test public class JunitTestSuite { } 然后用JunitCore就可以跑起來了參數(shù)化測(cè)試
示例代碼(校驗(yàn)是否是質(zhì)數(shù)的參數(shù)化測(cè)試)Junit 4 has introduced a new feature Parameterized tests.Parameterized
tests allow developer to run the same test over and over again using
different values. There are five steps, that you need to follow to
create Parameterized tests.
// 工具類 public class PrimeNumberChecker { public boolean validate(final Integer primeNumber){ for(int i = 2; i<(primeNumber / 2); i++){ if(primeNumber % i == 0){ return false; } } return true; } }
import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; //步驟一:為準(zhǔn)備使用參數(shù)化測(cè)試的測(cè)試類指定特殊的運(yùn)行器 @RunWith(Parameterized.class) public class PrimeNumberCheckTest { //步驟二:為測(cè)試類聲明幾個(gè)變量,分別用于存放期望值和測(cè)試所用數(shù)據(jù)。 private Integer inputNumber; private Boolean expectedResult; private PrimeNumberChecker primeNumberChecker; @Before public void initialize() { primeNumberChecker = new PrimeNumberChecker(); } //步驟三:為測(cè)試類聲明一個(gè)帶有參數(shù)的公共構(gòu)造函數(shù),并在其中為第二個(gè)環(huán)節(jié)中聲明的幾個(gè)變量賦值。 public PrimeNumberCheckTest(Integer inputNumber, Boolean expectedResult){ this.inputNumber = inputNumber; this.expectedResult = expectedResult; } //步驟四:為測(cè)試類聲明一個(gè)使用注解 org.junit.runners.Parameterized.Parameters 修飾的,返回值為 //java.util.Collection 的公共靜態(tài)方法,并在此方法中初始化所有需要測(cè)試的參數(shù)對(duì)。 @Parameterized.Parameters public static Collection
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64750.html
摘要:在類里,本地類里用關(guān)鍵字聲明過的方法,在單元測(cè)試啟動(dòng)后會(huì)自動(dòng)被調(diào)用到。在及的設(shè)定思路里,放在路徑下面以結(jié)尾的類會(huì)被當(dāng)成單元測(cè)試類處理。 ABAP 在ABAP類里,本地類(Local Class)里用關(guān)鍵字FOR TESTING聲明過的方法,showImg(https://segmentfault.com/img/remote/1460000016898407); 在單元測(cè)試啟動(dòng)后會(huì)自動(dòng)...
摘要:它由和建立,逐漸成為源于的的家族中最為成功的一個(gè)。與添加進(jìn)入的的依賴中。具有兩個(gè)參數(shù)可選該測(cè)試方法允許執(zhí)行的最大時(shí)間長(zhǎng)度。單位捕獲拋出的異常。這個(gè)類不包含任何方法更改入口類的測(cè)試運(yùn)行器為將要運(yùn)行的測(cè)試類作為數(shù)組傳入到中。 簡(jiǎn)介 JUnit是一個(gè)Java語言的單元測(cè)試框架。它由Kent Beck和Erich Gamma建立,逐漸成為源于Kent Beck的sUnit的xUnit家族中最為...
摘要:會(huì)把真實(shí)值乘以這個(gè)因子后存儲(chǔ),取出時(shí)再還原。日期類型可以對(duì)日期格式化為字符串存儲(chǔ),但是建議我們存儲(chǔ)為毫秒值,存儲(chǔ)為,節(jié)省空間。 最近在學(xué)習(xí)es,起碼要先有個(gè)es環(huán)境吧,然后再是整合到代碼中使用一下,畢竟只有實(shí)踐才會(huì)有深刻的記憶,這就是所謂的經(jīng)驗(yàn)啊,下面開始吧,本文分兩部分,第一部分配置es環(huán)境,第二部分整合到springboot中進(jìn)行簡(jiǎn)單的操作,本人也是初次學(xué)習(xí),如有錯(cuò)誤歡迎指出修正,...
摘要:本周在寫單元測(cè)試的時(shí)候遇見了一個(gè)新的,在此記錄一下。通過查看的源碼果然是這樣沒有重寫的但為什么會(huì)調(diào)用方法呢 本周在寫單元測(cè)試的時(shí)候遇見了一個(gè)新的exception,在此記錄一下。 單元測(cè)試中有一段代碼是這樣的: logger.debug(設(shè)置班級(jí)的學(xué)生); klass.setStudentList(Collections.singletonList(student1)); ...
摘要:當(dāng)面講給你聽講堂地址,或許是最實(shí)用的教程,新課促銷中,只要你敢來,保你收貨滿滿。優(yōu)惠報(bào)名全程擼碼快速入門教程全原價(jià),優(yōu)惠價(jià)全程擼碼進(jìn)階全原價(jià),優(yōu)惠價(jià) 回顧 Spring Boot - 初識(shí) Hello World Spring Boot - Servlet、過濾器、監(jiān)聽器、攔截器 Spring Boot - 靜態(tài)資源處理、啟動(dòng)加載、日志處理 Spring Boot - 部署Deplo...
閱讀 5129·2023-04-25 18:47
閱讀 2745·2021-11-19 11:33
閱讀 3495·2021-11-11 16:54
閱讀 3156·2021-10-26 09:50
閱讀 2623·2021-10-14 09:43
閱讀 737·2021-09-03 10:47
閱讀 737·2019-08-30 15:54
閱讀 1565·2019-08-30 15:44