相关疑难解决方法(0)

如何测试没有抛出异常?

我知道一种方法是:

@Test
public void foo(){
   try{
      //execute code that you expect not to throw Exceptions.
   }
   catch(Exception e){
      fail("Should not have thrown any exception");
   }
}
Run Code Online (Sandbox Code Playgroud)

有没有更清洁的方法来做到这一点.(可能使用Junit的@Rule?)

java junit unit-testing exception-handling junit4

206
推荐指数
10
解决办法
18万
查看次数

如何对 void 函数进行单元测试?

class Elephant extends Animal {   
    public Elephant(String name) {
        super(name);
    }

    void makeNoise() {
        logger.info(" Elephant  make Sound");
    }

    void perform(String day) {
        if (day.equals("thursday") || day.equals("friday")) {
            makeNoise();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想测试一下这个perform方法。如何使用 JUnit 对该方法进行单元测试?

java junit unit-testing

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×2

junit ×2

unit-testing ×2

exception-handling ×1

junit4 ×1