Java Course Learning Note-junit accuracy/failure/stress test difference

Source: Internet
Author: User
Tags assert

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.