在JUnit中,何时应该使用assertEquals()何时应该使用try…catch来验证结果?

在JUnit中,何时应该使用assertEquals()何时应该使用try…catch来验证结果?,junit,Junit,e、 g,在下面代码的verifysthissetRightly()中,我应该使用assertEquals()检查结果,还是应该抛出异常以便调用方的try…catch捕获它并让调用方处理 @参数 公共静态集合methodParams(){ List params=new ArrayList(); /*arg,errorString*/ add(新对象[]{“arg1”,null/*errorString*/}); 添加(新对象[]{“arg2”,错误_STRING1/*errorString*/

e、 g,在下面代码的
verifysthissetRightly()
中,我应该使用
assertEquals()
检查结果,还是应该抛出异常以便调用方的
try…catch
捕获它并让调用方处理

@参数
公共静态集合methodParams(){
List params=new ArrayList();
/*arg,errorString*/
add(新对象[]{“arg1”,null/*errorString*/});
添加(新对象[]{“arg2”,错误_STRING1/*errorString*/});
}
@试验
公共无效样本测试(){
试一试{
方法和等待(arg);
assertNull(“预期错误字符串”+错误字符串,错误字符串);
}捕获(例外情况除外){
assertNotNull(“预期错误字符串”+错误字符串,错误字符串);
assertTrue(例如getMessage().contains(errorString));
}
}
私有void MethodAndWait(){
调用远程服务器设置;
验证此设置是否正确();
}
私有void VerifySetRightly(){
int sth=getSth();
资产质量(sth=“5”);
}
您的测试应该是

@Test
public void sampleTest () {
    call_remote_server_to_have_sth_set;
    int sth = getSth();
    assertEquals(5, sth);
}
如果您还没有完成JUnit测试,我建议您阅读JUnit测试简介。

您的测试应该是

@Test
public void sampleTest () {
    call_remote_server_to_have_sth_set;
    int sth = getSth();
    assertEquals(5, sth);
}

如果您还没有完成JUnit测试,我建议您阅读JUnit测试简介。

在JUnit测试中,您应该使用类似于
assertEquals()
的断言来验证方法调用的结果或对象的状态:

@测试
public void addingTwoNumbersShouldWork(){
int结果=计算器。加(5,7);
资产质量(12,结果);
assertFalse(calculator.hasOverflow());
}
在JUnit测试中使用
try
catch
来测试代码块抛出预期异常以外的任何内容是极其罕见的:

@测试
public void setColorShouldThrowNullPointerExceptionOnNullInput(){
试一试{
deathRay.setColor(空);
失败(“预期的NullPointerException”);
}捕获(应为NullPointerException){
资产(应为.getMessage(),包含(“死亡光线颜色”);
}
}
如果正在测试的方法碰巧抛出异常,则不需要使用
try
catch

@测试
public void fireDeathRay()抛出DeathRayException{
deathRay.fire();
}
在上述测试中,如果
fire()
抛出
deatrayexception
(或运行时异常),
firedatray
测试将失败


在JUnit4中,使用
try
catch
更为罕见,因为在JUnit测试中,您应该使用
assertEquals()
之类的断言来验证方法调用的结果或对象的状态:

@测试
public void addingTwoNumbersShouldWork(){
int结果=计算器。加(5,7);
资产质量(12,结果);
assertFalse(calculator.hasOverflow());
}
在JUnit测试中使用
try
catch
来测试代码块抛出预期异常以外的任何内容是极其罕见的:

@测试
public void setColorShouldThrowNullPointerExceptionOnNullInput(){
试一试{
deathRay.setColor(空);
失败(“预期的NullPointerException”);
}捕获(应为NullPointerException){
资产(应为.getMessage(),包含(“死亡光线颜色”);
}
}
如果正在测试的方法碰巧抛出异常,则不需要使用
try
catch

@测试
public void fireDeathRay()抛出DeathRayException{
deathRay.fire();
}
在上述测试中,如果
fire()
抛出
deatrayexception
(或运行时异常),
firedatray
测试将失败


在JUnit4中,使用
try
catch
更为罕见,因为。

在我的答案被接受后,我将采取不同寻常的步骤添加另一个答案。前面的回答集中在总结中提出的问题上,但我想集中在代码上

我认为您想知道该做什么的原因之一是因为test
sampleTest()
正在测试两个完全不同的东西您的测试方法是在同一测试方法中测试正常行为和异常行为。

相反,将异常情况的测试划分为他们自己的测试方法。例如:

@RunWith(JUnit4.class)
公共类抽样测试{
@试验
public void methodAndWaitShouldAcceptNonNullValue(){
ClassUnderTest.methodAndWait(“arg1”)
}
@试验
public void method和waitshouldthrowhengivennullvalue(){
试一试{
ClassUnderTest.methodAndWait(null);
失败(“未引发NullPointerException”);
}捕获(NullPointerException ex){
assertTrue(例如getMessage().contains(错误1));
}
}
}
这有几个优点:

  • 如果
    methodAndWait(“arg1”)
    抛出异常,测试将失败,并出现有用的堆栈跟踪
  • 如果
    methodAndWait(null)
    抛出的不是
    NullPointerException
    ,测试将失败,并出现有用的堆栈跟踪
  • 如果
    methodAndWait(null)
    没有抛出任何东西,测试将失败并显示有用的消息
  • 这两项测试的目的都非常明确
  • 如果需要使用多个参数进行测试,可以使用附带的
    运行程序:

    @RunWith(封闭的.class)
    公共类抽样测试{
    @RunWith(参数化的.class)
    分配非空时的公共静态类{
    @参数
    公共静态集合methodParams(){
    List params=new ArrayList();
    /*arg,errorString*/
    add(新对象[]{“arg1”,“result1”});
    添加(新对象[]{“arg3”,“result3”});
    }
    公共最终字符串arg;
    公共最终字符串actualResult;
    公共样本测试(字符串参数、字符串实际结果){
    this.arg=arg;
    th