Accuracy test (result accuracy testing)
For example, Assert.assertequals (expected, actual).
If the result does not meet the expectations, it produces failure. Indicates a problem with the program logic.
Failure test (throwing exception tests)
The expected property is used to indicate the type of exception expected to be thrown. For example, @Test (expected = illegalargumentexception.class).
If the result does not meet the expectations, it produces failure. Indicates a problem with the program logic.
Stress test (run-time testing)
The Timeout property is used to indicate the time limit, in milliseconds. For example, @Test (timeout = 300).
Error occurs if the timeout occurs. The performance of the program itself is not up to the requirements.
As an example,
1 Packageedu.zju.cst.Student;2 3 Public classStudent {4 PrivateString name;5 6 PublicString GetName () {7 returnname;8 }9 Ten Public voidsetName (String name) { One This. Name =name; A } - - Publicstring Speak (String stm) { the Try { -Thread.Sleep (400); -}Catch(interruptedexception e) { - e.printstacktrace (); + } - + returnName + ":" +stm; A } at}
Student.java
1 PackageEDU.ZJU.CST;2 3 ImportOrg.junit.After;4 ImportOrg.junit.Before;5 Importorg.junit.Test;6 7 Importedu.zju.cst.Student.Student;8 ImportJunit.framework.Assert;9 Ten Public classStudentaccuracytest { One PrivateStudent Student; A - @Before - Public voidsetUp () { theStudent =NewStudent (); -Student.setname ("Tom"); - } - + @Test - Public voidTestspeak () { +String expected = "Tom:hello"; AString actual = Student.speak ("Hell"); at Assert.assertequals (expected, actual); - } - - @After - Public voidTearDown () { - in } -}
Studentaccuracytest.java
1 PackageEDU.ZJU.CST;2 3 ImportOrg.junit.After;4 ImportOrg.junit.Before;5 Importorg.junit.Test;6 7 Importedu.zju.cst.Student.Student;8 9 Public classStudentfailuretest {Ten PrivateStudent Student; One A @Before - Public voidsetUp () { -Student =NewStudent (); theStudent.setname ("Tom"); - } - -@Test (expected = illegalargumentexception.class) + Public voidTestspeak ()throwsIllegalArgumentException { -Student.speak ("Hell"); + } A at @After - Public voidTearDown () { - - } -}
Studentfailuretest.java
1 PackageEDU.ZJU.CST;2 3 ImportOrg.junit.After;4 ImportOrg.junit.Before;5 Importorg.junit.Test;6 7 Importedu.zju.cst.Student.Student;8 9 Public classStudentstresstest {Ten PrivateStudent Student; One A @Before - Public voidsetUp () { -Student =NewStudent (); theStudent.setname ("Tom"); - } - -@Test (Timeout = 300) + Public voidTestspeak () { -Student.speak ("Hell"); + } A at @After - Public voidTearDown () { - - } -}
Studentstresstest.java
1 PackageEDU.ZJU.CST;2 3 ImportOrg.junit.runner.RunWith;4 ImportOrg.junit.runners.Suite;5 Importorg.junit.runners.Suite.SuiteClasses;6 7@RunWith (Suite.class)8@SuiteClasses ({studentaccuracytest.class, Studentfailuretest.class,9Studentstresstest.class})Ten Public classTestSuite { One A}
Testsuite.java
Resources
Summary of several rules of JUnit unit testing
Junit4:test Two properties for annotations: expected and timeout
Java Course Learning Note-junit accuracy/failure/stress test difference