ตัวอย่างการเขียน Spring-boot WebFlux Unit Test
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 ก็ได้)
@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