期望代码引发可抛出的 spring JUnit
Expecting code to raise a throwable spring JUnit
我正在使用 Java 单元测试。当我们正确地给出参数时,单元测试应该没有任何错误(绿色)。但是我收到这样的错误。 Java 版本 1.8
Expecting code to raise a throwable.
TicTacToeService
public class TicTacToeService {
public void play(int x, int y) {
if(x<0)
throw new IllegalArgumentException("x cannot be negative");
if (y < 0)
throw new IllegalArgumentException("y cannot be negative");
if (x > 2)
throw new IllegalArgumentException("x cannot be greater than 2");
if (y > 2)
throw new IllegalArgumentException("y cannot be greater than 2");
}
}
井字棋测试
public class TicTacToeTest {
private TicTacToeService ticTacToeService = new TicTacToeService();
@Test
public void givenXIsNegativeExpectException(){
int x = -1;
int y = 1;
Assertions.assertThatThrownBy(() -> ticTacToeService.play(x, y))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("X cannot be negative");
}
@Test
public void givenYIsNegativeExpectException(){
int x = 1;
int y = -1;
Assertions.assertThatThrownBy(() -> ticTacToeService.play(x, y))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("y cannot be negative");
}
}
依赖项
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation("org.modelmapper:modelmapper:2.3.7")
implementation 'org.springframework.boot:spring-boot-starter-validation'
testCompile group: 'junit', name: 'junit', version: '4.4'
implementation 'org.liquibase:liquibase-core'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.1'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0'
}
问题是什么?请帮助我
我怀疑你在使用 assertThatThrownBy
但实际上你想使用 assertThrows
。例如,您可以查看此 link。
我可以声明我自己的自定义运行时异常,如下所示
服务class
@Service
public class TicTacToeService {
public void play(int x, int y) {
if (x < 0)
throw new XNegativeException("x cannot be negative");
if (y < 0)
throw new YNegativeException("y cannot be negative");
if (x > 2)
throw new XLimitException("x cannot be greater than 2");
if (y > 2)
throw new YLimitException("y cannot be greater than 2");
}
static class XNegativeException extends RuntimeException{
public XNegativeException(String message) {
super(message);
}
}
static class YNegativeException extends RuntimeException{
public YNegativeException(String message) {
super(message);
}
}
static class XLimitException extends RuntimeException{
public XLimitException(String message) {
super(message);
}
}
static class YLimitException extends RuntimeException{
public YLimitException(String message) {
super(message);
}
}
}
测试文件
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class TicTacToeServiceTest {
private final TicTacToeService ticTacToeService = new TicTacToeService();
@Test
void testXIsNegative() {
Assertions.assertThrows(TicTacToeService.XNegativeException.class,
() -> ticTacToeService.play(-1, 0));
}
@Test
void testYIsNegative() {
Assertions.assertThrows(TicTacToeService.YNegativeException.class,
() -> ticTacToeService.play(0, -898));
}
@Test
void testXLimitReached() {
Assertions.assertThrows(TicTacToeService.XLimitException.class,
() -> ticTacToeService.play(3, 0));
}
@Test
void testYLimitReached() {
Assertions.assertThrows(TicTacToeService.YLimitException.class,
() -> ticTacToeService.play(0, 3));
}
}
我正在使用 Java 单元测试。当我们正确地给出参数时,单元测试应该没有任何错误(绿色)。但是我收到这样的错误。 Java 版本 1.8
Expecting code to raise a throwable.
TicTacToeService
public class TicTacToeService {
public void play(int x, int y) {
if(x<0)
throw new IllegalArgumentException("x cannot be negative");
if (y < 0)
throw new IllegalArgumentException("y cannot be negative");
if (x > 2)
throw new IllegalArgumentException("x cannot be greater than 2");
if (y > 2)
throw new IllegalArgumentException("y cannot be greater than 2");
}
}
井字棋测试
public class TicTacToeTest {
private TicTacToeService ticTacToeService = new TicTacToeService();
@Test
public void givenXIsNegativeExpectException(){
int x = -1;
int y = 1;
Assertions.assertThatThrownBy(() -> ticTacToeService.play(x, y))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("X cannot be negative");
}
@Test
public void givenYIsNegativeExpectException(){
int x = 1;
int y = -1;
Assertions.assertThatThrownBy(() -> ticTacToeService.play(x, y))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("y cannot be negative");
}
}
依赖项
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation("org.modelmapper:modelmapper:2.3.7")
implementation 'org.springframework.boot:spring-boot-starter-validation'
testCompile group: 'junit', name: 'junit', version: '4.4'
implementation 'org.liquibase:liquibase-core'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.1'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0'
}
问题是什么?请帮助我
我怀疑你在使用 assertThatThrownBy
但实际上你想使用 assertThrows
。例如,您可以查看此 link。
我可以声明我自己的自定义运行时异常,如下所示
服务class
@Service
public class TicTacToeService {
public void play(int x, int y) {
if (x < 0)
throw new XNegativeException("x cannot be negative");
if (y < 0)
throw new YNegativeException("y cannot be negative");
if (x > 2)
throw new XLimitException("x cannot be greater than 2");
if (y > 2)
throw new YLimitException("y cannot be greater than 2");
}
static class XNegativeException extends RuntimeException{
public XNegativeException(String message) {
super(message);
}
}
static class YNegativeException extends RuntimeException{
public YNegativeException(String message) {
super(message);
}
}
static class XLimitException extends RuntimeException{
public XLimitException(String message) {
super(message);
}
}
static class YLimitException extends RuntimeException{
public YLimitException(String message) {
super(message);
}
}
}
测试文件
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class TicTacToeServiceTest {
private final TicTacToeService ticTacToeService = new TicTacToeService();
@Test
void testXIsNegative() {
Assertions.assertThrows(TicTacToeService.XNegativeException.class,
() -> ticTacToeService.play(-1, 0));
}
@Test
void testYIsNegative() {
Assertions.assertThrows(TicTacToeService.YNegativeException.class,
() -> ticTacToeService.play(0, -898));
}
@Test
void testXLimitReached() {
Assertions.assertThrows(TicTacToeService.XLimitException.class,
() -> ticTacToeService.play(3, 0));
}
@Test
void testYLimitReached() {
Assertions.assertThrows(TicTacToeService.YLimitException.class,
() -> ticTacToeService.play(0, 3));
}
}