【问题标题】:Expecting code to raise a throwable spring JUnit期望代码引发一个可抛出的 spring JUnit
【发布时间】:2021-02-05 09:41:55
【问题描述】:

我正在使用 Java 单元测试。当我们正确地给出参数时,单元测试应该没有任何错误(绿色)。但是我遇到了这样的错误。 Java 1.8 版

Expecting code to raise a throwable.

井字游戏服务

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'
}

有什么问题?请帮帮我

【问题讨论】:

    标签: spring-boot unit-testing junit


    【解决方案1】:

    我怀疑您使用的是assertThatThrownBy,但实际上您想使用assertThrows。例如,您可以查看link

    【讨论】:

      【解决方案2】:

      我可以声明我自己的自定义运行时异常,如下所示

      服务等级

      @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));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-03-16
        • 2010-10-26
        • 2020-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多