Spring boot 期望代码引发一个可丢弃的spring JUnit
我正在使用Java单元测试。当我们正确地给出参数时,单元测试应该不会出现任何错误(绿色)。但是我犯了这样一个错误。Java版本1.8Spring boot 期望代码引发一个可丢弃的spring JUnit,spring-boot,unit-testing,junit,Spring Boot,Unit Testing,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 IllegalArgumentE
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");
}
}
依赖关系
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'
}
有什么问题?请帮助我我怀疑您正在使用Thrownby的
资产,但实际上,您想使用资产抛出
。例如,您可以对此进行检查。我怀疑您正在使用资产thrownby
,但实际上,您希望使用资产thrownby
。例如,您可以对此进行检查。我可以声明自己的自定义运行时异常,并按如下方式编写
服务等级
@服务
公共服务{
公共无效播放(整数x,整数y){
if(x<0)
抛出新的XNegativeException(“x不能为负”);
if(y<0)
抛出新的YNegativeException(“y不能为负”);
如果(x>2)
抛出新的XLimitException(“x不能大于2”);
如果(y>2)
抛出新的YLimitException(“y不能大于2”);
}
静态类XNegativeException扩展了RuntimeException{
公共XNegativeException(字符串消息){
超级(信息);
}
}
静态类YNegativeException扩展了RuntimeException{
公共YNegativeException(字符串消息){
超级(信息);
}
}
静态类XLimitException扩展了RuntimeException{
公共XLimitException(字符串消息){
超级(信息);
}
}
静态类YLimitException扩展RuntimeException{
公共YLimitException(字符串消息){
超级(信息);
}
}
}
测试文件
import org.junit.jupiter.api.Assertions;
导入org.junit.jupiter.api.Test;
类TictoeServiceTest{
私有最终TicTacToeService TicTacToeService=新的TicTacToeService();
@试验
void testXIsNegative(){
assertThrows(TicTacToeService.XNegativeException.class,
()->ticTacToeService.play(-1,0));
}
@试验
void testyis负(){
assertThrows(TicTacToeService.YNegativeException.class,
()->ticTacToeService.play(0,-898));
}
@试验
void testXLimitReached(){
assertThrows(TicTacToeService.XLimitException.class,
()->ticTacToeService.play(3,0));
}
@试验
void testYLimitReached(){
assertThrows(TicTacToeService.YLimitException.class,
()->ticTacToeService.play(0,3));
}
}
我可以声明自己的自定义运行时异常,并按如下方式编写
服务等级
@服务
公共服务{
公共无效播放(整数x,整数y){
if(x<0)
抛出新的XNegativeException(“x不能为负”);
if(y<0)
抛出新的YNegativeException(“y不能为负”);
如果(x>2)
抛出新的XLimitException(“x不能大于2”);
如果(y>2)
抛出新的YLimitException(“y不能大于2”);
}
静态类XNegativeException扩展了RuntimeException{
公共XNegativeException(字符串消息){
超级(信息);
}
}
静态类YNegativeException扩展了RuntimeException{
公共YNegativeException(字符串消息){
超级(信息);
}
}
静态类XLimitException扩展了RuntimeException{
公共XLimitException(字符串消息){
超级(信息);
}
}
静态类YLimitException扩展RuntimeException{
公共YLimitException(字符串消息){
超级(信息);
}
}
}
测试文件
import org.junit.jupiter.api.Assertions;
导入org.junit.jupiter.api.Test;
类TictoeServiceTest{
私有最终TicTacToeService TicTacToeService=新的TicTacToeService();
@试验
void testXIsNegative(){
assertThrows(TicTacToeService.XNegativeException.class,
()->ticTacToeService.play(-1,0));
}
@试验
void testyis负(){
assertThrows(TicTacToeService.YNegativeException.class,
()->ticTacToeService.play(0,-898));
}
@试验
void testXLimitReached(){
assertThrows(TicTacToeService.XLimitException.class,
()->ticTacToeService.play(3,0));
}
@试验
void testYLimitReached(){
assertThrows(TicTacToeService.YLimitException.class,
()->ticTacToeService.play(0,3));
}
}
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'
}