[Introduction to JUnit Practice](https://www.amazon.co.jp/JUnit%E5%AE%9F%E8%B7%B5%E5%85%A5%E9%96%80-~%E4%BD% 93% E7% B3% BB% E7% 9A% 84% E3% 81% AB% E5% AD% A6% E3% 81% B6% E3% 83% A6% E3% 83% 8B% E3% 83% 83% E3% 83% 88% E3% 83% 86% E3% 82% B9% E3% 83% 88% E3% 81% AE% E6% 8A% 80% E6% B3% 95-WEB-PRESS-plus / dp / If you have 477415377X), there is nothing wrong with it, but since I often don't have it, I'll summarize only the ones I use often.
@Test
public void test1() {
String actual = "hoge";
String expected = "hoge";
assertThat(actual, is(expected));
}
@Test
public void test2() {
String actual = null;
assertThat(actual, is(nullValue()));
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
The method annotated with @Test
is the actual test method. ʻAssertThat () compares the expected value with the actual value. In the example, ʻis ()
and nullValue ()
are used, but other things such as not ()
are used.
@ Before
is executed before each method of @ Test
is executed, and @ After
is executed after each method of @ Test
is executed. If there is a process common to each test, such as connecting and disconnecting to the DB, you can write it here.
@BeforeClass
is executed only once before all @ Test
methods are executed, and @ AfterClass
is executed only once after all @ Test
methods are executed. @BeforeClass
is used when picking up test parameters from property files. I don't think I use @AfterClass
very much ...
In the example, nothing is written like @Before
, but you can write a test here with ʻassertThat ()`. You can also test the state before and after each process.
@RunWith(Enclosed.class)
public class JUnitTest {
public static class TestClass1 {
@Before
public void setUp() throws Exception {
//Pre-processing for Test Class 1
}
@After
public void tearDown() throws Exception {
//Post-processing for Test Class 1
}
@Test
public void test() {
String actual = "hoge";
String expected = "hoge";
assertThat(actual, is(expected));
}
}
public static class TestClass2 {
@Before
public void setUp() throws Exception {
//Pre-processing for Test Class 2
}
@After
public void tearDown() throws Exception {
//Post-processing for Test Class 2
}
@Test
public void test() {
String actual = "fuge";
String expected = "fuge";
assertThat(actual, is(expected));
}
}
}
If the pre-processing is different for each test, add @RunWith (Enclosed.class)
to the test class and then create an inner class.
In the example, TestClass1
andTestClass2
can execute different pre-processing and post-processing.
It's convenient, but I forget how to write the annotation @RunWith (Enclosed.class)
every time.
@RunWith(Theories.class)
public class JUnitTest {
@DataPoints
public static String[] actualValues = {"hoge", "fuge"};
@Theory
public void test(String actualValue) {
int actual = actualValue.length();
int expected = 4;
assertThat(actual, is(expected));
}
}
If there are multiple tests with similar input and output formats, it is better to put them together to some extent.
Specify @RunWith (Theories.class)
for the test class and @Theory
instead of @Test
for the test method. If you put the test input and output in an array of @ DataPoints
and specify the value of the type specified by @ DataPoints
in the argument of the method of @ Theory
, it will be @ Theory
When the method is executed, the test is executed for the number of arrays of @ DataPoints
.
Recommended Posts