- Mastering Software Testing with JUnit 5
- Boni García
- 511字
- 2021-07-02 15:34:27
Standard tests in JUnit 4
In JUnit 4, the @Test annotation (contained in package org.junit) represents a test. Any public method can be annotated with @Test to make it a test method.
In order to set up the test fixture, JUnit 4 provides the @Before annotation. This annotation can be used in any public method. Similarly, any public method annotated with @After gets executed after each test method execution. JUnit 4 provides two more annotations to enhance the test life cycle: @BeforeClass and @AfterClass. They are executed only once per test class, before and after all tests, respectively. The following picture depicts the life cycle of a JUnit 4 test case:

The following table summarizes the main differences between JUnit 3 and JUnit 4 seen so far:

The org.junit.Assert class provides static methods to carry out assertions (predicates). The following are the most useful assertion methods:
- assertTrue: If the condition becomes false, the assertion fails and AssertionError is thrown.
- assertFalse: If the condition becomes true, the assertion fails and AssertionError is thrown.
- assertNull: This checks whether the argument is null, otherwise throws AssertionError if the argument is not null.
- assertNotNull: This checks whether the argument is not null; otherwise, it throws AssertionError
- assertEquals: This compares two objects or primitive types. Moreover, if the actual value doesn't match the expected value, AssertionError is thrown.
- assertSame: This supports only objects and checks the object reference using the == operator.
- assertNotSame: This is the opposite of assertSame.
The following snippets provide a simple example of a JUnit 4 test case. As we can see, it is the equivalent test case as seen in the previous section, this time using the JUnit 4 programming model, that is, using @Test annotation to identify tests and other annotations (@AfterAll, @After, @BeforeAll, @Before) to implement the test life cycle (setup and teardown test fixture):
package io.github.bonigarcia;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestSimple {
// Phase 1.1: Setup (for all tests)
@BeforeClass
public static void setupAll() {
System.out.println("<Setup Class>");
}
// Phase 1.2: Setup (for each test)
@Before
public void setupTest() {
System.out.println("<Setup Test>");
}
// Test 1: This test is going to succeed
@Test
public void testSuccess() {
// Phase 2: Simulation of exercise
int expected = 60;
int real = 60;
System.out.println("** Test 1 **");
// Phase 3: Verify
assertEquals(expected + " should be equals to "
+ real, expected, real);
}
// Test 2: This test is going to fail
@Test
public void testFailure() {
// Phase 2: Simulation of exercise
int expected = 60;
int real = 20;
System.out.println("** Test 2 **");
// Phase 3: Verify
assertEquals(expected + " should be equals to "
+ real, expected, real);
}
// Phase 4.1: Teardown (for each test)
@After
public void teardownTest() {
System.out.println("</Ending Test>");
}
// Phase 4.2: Teardown (for all test)
@AfterClass
public static void teardownClass() {
System.out.println("</Ending Class>");
}
}
- Go Web編程
- Oracle從新手到高手
- Learning Elixir
- Data Analysis with IBM SPSS Statistics
- Visual Basic程序設(shè)計(jì)習(xí)題解答與上機(jī)指導(dǎo)
- Mastering Yii
- 網(wǎng)店設(shè)計(jì)看這本就夠了
- Oracle從入門到精通(第5版)
- 零基礎(chǔ)學(xué)單片機(jī)C語(yǔ)言程序設(shè)計(jì)
- Java Fundamentals
- Hands-On Nuxt.js Web Development
- 計(jì)算機(jī)應(yīng)用技能實(shí)訓(xùn)教程
- C++從入門到精通(第6版)
- HTML+CSS+JavaScript網(wǎng)頁(yè)制作:從入門到精通(第4版)
- JavaScript編程精解(原書第2版)