ตัวอย่างการเขียน Spring-boot WebFlux Test Coverage
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.12.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
</dependencies>
...
- junit เป็น dependency สำหรับเขียน test ภาษา java
- assertj เป็น dependency สำหรับทำ assert (support junit ซึ่งจริง ๆ ใช้แค่ junit ก็ได้)
pom.xml
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
@SpringBootApplication
@ComponentScan(basePackages = {"com.pamarin"})
public class AppStarter {
public static void main(String[] args) {
SpringApplication.run(AppStarter.class, args);
}
}
public class ByteUtils {
private ByteUtils() {
}
...
}
public class ByteUtils_xorTest {
/*
* A | B | answer
* --------------
* 0 | 0 | 0
* 0 | 1 | 1
* 1 | 0 | 1
* 1 | 1 | 0
*/
@Test
public void shouldBe00000000() {
byte[] input1 = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
byte[] input2 = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
byte[] output = ByteUtils.xor(input1, input2);
byte[] expected = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
assertThat(output).isEqualTo(expected);
}
...
cd ไปที่ root ของ project จากนั้น
$ mvn clean install
โดยไปที่ root project จากนั้นเปิดไฟล์ target/jacoco-ut/index.html
ขึ้นมาดู จะได้ผลลัพธ์เป็น