In the previous chapter, we shared “What is JUnit?” this chapter will begin with “How to write tests with JUnit.” We’ll start with a “simple test” example.
Write Test methods in a Test class, typically annotating them as @test, @REPEATedTest, @parameterizedTest,@TestFactory, or @testTemplate. So let’s start at @test.
In most cases, it is enough to define the Test method with @test, plus the four life cycle methods @beforeeach, @Aftereach, @beforeAll, and @Afterall. So, if you don’t have enough time, just study the first two sections of this chapter and save the rest for later.
Importance: ★★★★★
Here is a test class ConfigurationTest that contains two test methods:
package yang.yu.configuration; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; class ConfigurationTest { @TempDir Path tempDir; /** * Key exists,value exists, should return value */ @test void get_string_without_defaultValue_happy() throws Exception {Path file = tempDir.resolve("conf.properties");
Files.write(file, Arrays.asList("birthday=2002-05-11"."size=15"."closed=true"."locked=false"."Salary = 12.5"."Name =" Tom"."noneValue="));
Configuration instance = Configuration.builder()
.fromFile(file.toFile())
.dateFormat("yyyy-MM-dd")
.build();
assertThat(instance.getString("name")).isEqualTo("Zhang"); } /** * key exists, Value exists and the format is correct. Value */ @test void get_int_with_defaultValue_and_with_value() throws Exception {Path file = tempDir.resolve("conf.properties");
Files.write(file, Arrays.asList("birthday=2002-05-11"."size=15"."closed=true"."locked=false"."Salary = 12.5"."Name =" Tom"."noneValue="));
Configuration instance = Configuration.builder()
.fromFile(file.toFile())
.dateFormat("yyyy-MM-dd")
.build();
assertThat(instance.getInt("size", 1000)).isEqualTo(15); }}Copy the code
The instructions are as follows:
Methods annotated as @test are Test methods. There are two test methods: get_string_without_defaultValue_happy() and get_int_with_defaultValue_and_with_value().
Because it has test methods, this class automatically becomes a test class, which is automatically recognized by ides such as Eclipse and IDEA and by build tools such as Maven and Gradle to execute tests.
This test class is used to test the Configuration class under test. Configuration reads the value of Configuration items from a text Configuration file.
A temporary directory is created with the @tempdir annotation (described in a later section). In both test methods, a conf.properties file is created in this temporary directory and multiple lines of configuration data are written to it. Test by creating an instance of the class Configuration under test from this Configuration file.
As you can see, there is a lot of duplicate code between the two test methods. Used to create instances of classes under test and prepare test data.
In the next section we eliminate these duplicates by using the “lifecycle approach”.