摘要:步驟三為測試類聲明一個帶有參數(shù)的公共構(gòu)造函數(shù),并在其中為第二個環(huán)節(jié)中聲明的幾個變量賦值。步驟五編寫測試方法,使用定義的變量作為參數(shù)進(jìn)行測試。
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.
// 測試case寫法1: // 繼承TestCase ,測試方法 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); } } // 測試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í)行測試用例 // 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()); } }愛上簡單注解
// 測試類 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)行這個測試: 控制臺輸出: in before class in before in test in after in after class豪華套裝(TestSuite)
先上來兩個測試用例 測試用例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); } }
測試用例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ù)化測試
示例代碼(校驗(yàn)是否是質(zhì)數(shù)的參數(shù)化測試)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ù)化測試的測試類指定特殊的運(yùn)行器 @RunWith(Parameterized.class) public class PrimeNumberCheckTest { //步驟二:為測試類聲明幾個變量,分別用于存放期望值和測試所用數(shù)據(jù)。 private Integer inputNumber; private Boolean expectedResult; private PrimeNumberChecker primeNumberChecker; @Before public void initialize() { primeNumberChecker = new PrimeNumberChecker(); } //步驟三:為測試類聲明一個帶有參數(shù)的公共構(gòu)造函數(shù),并在其中為第二個環(huán)節(jié)中聲明的幾個變量賦值。 public PrimeNumberCheckTest(Integer inputNumber, Boolean expectedResult){ this.inputNumber = inputNumber; this.expectedResult = expectedResult; } //步驟四:為測試類聲明一個使用注解 org.junit.runners.Parameterized.Parameters 修飾的,返回值為 //java.util.Collection 的公共靜態(tài)方法,并在此方法中初始化所有需要測試的參數(shù)對。 @Parameterized.Parameters public static Collection
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/64750.html
摘要:在類里,本地類里用關(guān)鍵字聲明過的方法,在單元測試啟動后會自動被調(diào)用到。在及的設(shè)定思路里,放在路徑下面以結(jié)尾的類會被當(dāng)成單元測試類處理。 ABAP 在ABAP類里,本地類(Local Class)里用關(guān)鍵字FOR TESTING聲明過的方法,showImg(https://segmentfault.com/img/remote/1460000016898407); 在單元測試啟動后會自動...
摘要:會把真實(shí)值乘以這個因子后存儲,取出時再還原。日期類型可以對日期格式化為字符串存儲,但是建議我們存儲為毫秒值,存儲為,節(jié)省空間。 最近在學(xué)習(xí)es,起碼要先有個es環(huán)境吧,然后再是整合到代碼中使用一下,畢竟只有實(shí)踐才會有深刻的記憶,這就是所謂的經(jīng)驗(yàn)啊,下面開始吧,本文分兩部分,第一部分配置es環(huán)境,第二部分整合到springboot中進(jìn)行簡單的操作,本人也是初次學(xué)習(xí),如有錯誤歡迎指出修正,...
摘要:本周在寫單元測試的時候遇見了一個新的,在此記錄一下。通過查看的源碼果然是這樣沒有重寫的但為什么會調(diào)用方法呢 本周在寫單元測試的時候遇見了一個新的exception,在此記錄一下。 單元測試中有一段代碼是這樣的: logger.debug(設(shè)置班級的學(xué)生); klass.setStudentList(Collections.singletonList(student1)); ...
摘要:當(dāng)面講給你聽講堂地址,或許是最實(shí)用的教程,新課促銷中,只要你敢來,保你收貨滿滿。優(yōu)惠報(bào)名全程擼碼快速入門教程全原價,優(yōu)惠價全程擼碼進(jìn)階全原價,優(yōu)惠價 回顧 Spring Boot - 初識 Hello World Spring Boot - Servlet、過濾器、監(jiān)聽器、攔截器 Spring Boot - 靜態(tài)資源處理、啟動加載、日志處理 Spring Boot - 部署Deplo...
閱讀 4913·2023-04-25 18:47
閱讀 2673·2021-11-19 11:33
閱讀 3445·2021-11-11 16:54
閱讀 3101·2021-10-26 09:50
閱讀 2540·2021-10-14 09:43
閱讀 665·2021-09-03 10:47
閱讀 671·2019-08-30 15:54
閱讀 1498·2019-08-30 15:44