[JAVA] Personal summary of the guys often used in JUnit 4

Introduction

[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.

Basic

@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.

When dividing the test into multiple parts


@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.

When repeating similar tests


@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

Personal summary of the guys often used in JUnit 4
Personal development: Summary of technologies used in comedy Gasha (scraping, Rails, Heroku)
[Github] Summary of commands often used during development
Summary of frequently used commands in Rails and Docker
Get the name of the test case in the JUnit test class
Gem often used in Rails
Matcher often used in RSpec
The case that @Autowired could not be used in JUnit5
Syntax examples often used in Java
About methods often used in devise
Summary of ORM "uroboroSQL" that can be used in enterprise Java
Test API often used in AssertJ
Summary of frequently used Docker commands
Mechanism and characteristics of Collection implementation class often used in Java
Expression used in the fizz_buzz problem
Commands often used in MySQL operations
Order of processing in the program
What are the rules in JUnit?
Ruby methods often used in Rails
Summary about the introduction of Device
Library summary that seems to be often used in recent Android development (2019/11)
Error when the member of Entity class used in SpringWebFlux is final
Specify the default JAVA_HOME used in buildship
Get the result of POST in Java
[Java] Personal summary of conditional statements (basic)
Summary of root classes in various languages
Summary of hashes and symbols in Ruby
[Personal] JUnit5 memorandum memo (work in progress)
The story of AppClip support in Anyca
The story of writing Java in Emacs
Write the movement of Rakefile in the runbook
If you want to satisfy the test coverage of private methods in JUnit
About the meaning of type variables, E, T, etc. used in generics used in Java
Summary of how to select elements in Selenium
[Order method] Set the order of data in Rails
[Ruby] Summary of class definitions. Master the basics.
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
[Java] Personal summary of classes and methods (basic)
Use the JDK used in Android Studio in the terminal
The story of learning Java in the first programming
Summary of new features added in Deeplearning4J 1.0.0-beta4
Overwrite the contents of config with Spring-boot + JUnit5
Feel the passage of time even in Java
Order of modifiers used in Swift (see SwiftLint)
Import files of the same hierarchy in Java
Summary of what I learned in Spring Batch
[Personal memorandum] Button layout created with javafx in Eclipse (in the case of HBox / VBox)
Summary of how to use the proxy set in IE when connecting with Java