JUnit Test related

Source: Internet
Author: User
Tags assert
Junit3.8

In Junit3.8, the test case must inherit testcase use TestCase to test the class

Public void testdevide () {

int result;

Try {

result = Cal.divide (15, 4);

Assert.assertequals (3, result);

Catch (Exception e) {

E.printstacktrace ();

Assert.fail ();

}

}

Public void Testdividebyzero () {

int result = 0;

Throwable ex = null;

String msg = null;

Try {

result = Cal.divide (10, 0);

Assert.fail ("Test failed,divided by 0");

Catch(Exception e) {

ex = e;

msg = E.getmessage ();

}

Assert.assertnotnull (ex);

Assert.assertequals ("divided by 0", msg);

Assert.assertequals (0, result);

automatically run multiple testcase using test suite

Public class Testall extends TestCase {

Public Static Test Suite () {

TestSuite ts = new TestSuite ();

Ts.addtestsuite (caculatortest. Class);

Ts.addtest ( new repeatedtest caculatortest ("Testdividebyzero"), 10);

return TS;

}

} JUNIT4

The test case in JUNIT4 does not need to inherit testcase, but is annotated with the annotation annotation.

In a test class, all the public void methods that are @test decorated are called testcase.

@Test Callout test method (public void) has attributes expected and timeout

Expected: Specifies the exception class, and the test succeeds if an exception is thrown

@Test (expected=exception. Class)

Public void Testcaculatedbyzero () throws exception{

Cal.divide (10, 0);

}

Timeout: The test fails if the test time exceeds the Timeout specified value

@Test (timeout=600)

Public void Testcaculator () {

int result = 0;

Try {

Thread.Sleep (500);

result = Cal.divide (15, 3);

catch(Exception ex) {

Assert.fail ();

}

Assertequals (5, result);

}

@Before Initialization Variable

To perform an action after @After test method

@BeforeClass Initialize global variables before all methods are executed

You can only modify a method that is public static void and has no parameters, and this method executes only once.

execute after @AfterClass all methods

@Ignore ignore a test so that the test does not execute, you can modify the method, class

Value: Reasons to ignore

@Ignore ("Not Ready") @Test (expected=exception. Class)

Public void Testcaculatedbyzero () throws exception{

Cal.divide (10, 0);

}

@RunWith (value= Runtime. Class) to specify that the runtime runs tests

Parameterized run test class

@RunWith (value=parameterized. Class)

Run as test suite

@RunWith (Suite. Class)

@SuiteClasses ({test1.class,test2.class,...})

@Parameters Cosmetic method to return test parameters

@Parameters

Public Static Collection Preparedata () {

object[][] data = {

{1,10,10},

{2,4,2},

{5,15,3},

{ -1,4,-4},

{0,0,3}

};

return Arrays.aslist (data);

}

Parameterized Run test Example:

Junit Runners (runtime):

Junit.awtui.TestRunner.run (Xxxtest.class);

Junit.swingui.TestRunner.run (Xxxtest.class);

Junit.textui.TestRunner.run (Xxxtest.class);

Related Article

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.