2017-03-14 178 views
1

这是我的问题:我已经提供了一个应用程序,并且必须编写测试用例才能使用JUnit进行测试。例如:使用属性String name实例化对象,并且此字段不能长于10个字符。我如何将其纳入测试方法?这里是我的代码: 要测试的类是:JUnit在测试中捕获异常

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

我的测试方法是:

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

} 

,异常是:

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

} 
+0

在您发布问题之前,请确保您已完成基本的谷歌搜索 – Mritunjay

+0

使用并尝试实施建议的解决方案,但未完成。 Ijust不知道该怎么办 – bogALT

+0

提示:在你的代码中,空的catch catch块(catch(HydricDSSException e){}')在测试中......究竟是应该做什么的?我的意思是:除了在你的抛出列表中声明要抛出的错误之外,还要默默地**掉**。换句话说:你的第一个bug已经在那里了。除此之外:空的抓块很少是一个好主意! – GhostCat

回答

0

你的问题是原来的构造。你在签名中有throws子句。你不应该在构造函数中有try/catch块。去掉它。

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

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

你有什么期望,如果名称为null抛出异常?这是允许的吗?你现在要得到一个空指针异常。

+0

我被要求在'public void testConstructor_NameTooLong()'后添加'抛出HydricDSSException'。可以吗? – bogALT

+0

是的,这是正确的。 – duffymo

+0

问题:像'NullPointerException'这样的异常是自动引发的,所以我只需要添加到我的测试代码中:'if(e instanceof NullPointerException){.....}','if(e instanceof HydricDSSException)'',right ?它接缝工作... – bogALT