关于java:JUnit在测试中捕获异常

JUnit catch exceptions in tests

本问题已经有最佳答案,请猛点这里访问。

这是我的问题:我得到了一个应用程序,我必须编写测试用例,以便使用JUnit测试它。例如:用属性String name实例化一个对象,这个字段不能长于tan 10个字符。我如何将其纳入测试方法?这是我的代码:测试等级为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package hdss.io;

import hdss.exceptions.HydricDSSException;

public class AquiferPublicData implements WaterResourceTypePublicData {
    private String myName;
    private float currentHeight;

    public AquiferPublicData (String name, float current)   throws HydricDSSException{

        try {
            if(name.length()>10) //name must be shorter than 11 chars
                throw new HydricDSSException("Name longer than 10");
            else{
                myName = name;      
                currentHeight = current;
            }
        } catch (HydricDSSException e) {            
        }
    }

    public String getMyName() {
        return myName;
    }
}

我的测试方法是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package hdss.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import hdss.exceptions.HydricDSSException;
import hdss.io.AquiferPublicData;

public class AquiferPublicDataTest {

    @Test
    public void testAquiferPublicData() {
        String notValidName ="this-name-is-too-long";
        try {
            AquiferPublicData apd = new AquiferPublicData(notValidName, 10);
            fail("Was supposed to throw Exception if name is longer than 10 chars");
        } catch (HydricDSSException e) {
            assertEquals("Name longer than 10", e.getMessage());
        }
    }

}

例外情况是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package hdss.exceptions;

public class HydricDSSException extends Exception{

    /**
     *
     */

    private static final long serialVersionUID = 1L;
    String message;

    //Esfuerzo Actual: 1.5 minutos

    public HydricDSSException (String message){

        this.message = message;
    }

    //Esfuerzo Actual: 1.5 minutos

    public String getMessage(){

        return this.message;
    }

}


您的问题是原始构造函数。签名中有throws子句。构造函数中不应该有try/catch块。除去它。

1
2
3
4
5
public AquiferPublicData (String name, float current)   throws HydricDSSException{
    if(name.length()>10) throw new HydricDSSException("Name longer than 10");
    myName = name;      
    currentHeight = current;
}

您应该编写一个JUnit测试来证明如果名称超过10,就会抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class AquiferPublicDataTest {

    @Test
    public void testConstructor_Success() {
        // setup
        String name ="Jones";
        // exercise
        AquiferPublicData data = new AquiferPublicData(name, 0.0);
        // assert
        Assert.assertEquals(name, data.getMyName());
    }

    @Test(expects = HydricDSSException.class)
    public void testConstructor_NameTooLong() throws HydricDSSException {
        // setup
        String name ="this one is too long";
        // exercise
        new AquiferPublicData(name, 0.0);
    }
}

如果名称为空,您期望得到什么?这是允许的吗?您将得到一个空指针异常。