我有一个JUnit 4测试,我正在使用https://code.google.com/p/catch-exception/库
@Test
public void testInvalidArgument() throws Exception{
verifyException(new Operation("" , "."), IllegalArgumentException.class);
verifyException(new Operation(null , null), NullPointerException.class);
}
它尝试验证构造函数抛出所述异常。但是我的测试总是通过抛出所述异常而不是通过验证异常来成功而失败。
我的课程是
@SuppressWarnings("serial")
@GwtCompatible
public class Operation implements Serializable {
private static Logger logger = Logger.getLogger(Operation.class.getName());
private String name;
private HashMap<String, Serializable> parameters;
private String interfaceName ;
/**
*
* @param full expects an fully qualified operation name e.g. interface.operation . Parses and separates the interface and operation names
*
* Throws Exception if either is null
*
*/
public Operation(final String full) throws IllegalArgumentException , NullPointerException {
interfaceName = checkValidInterfaceName(full.substring(0 , full.indexOf('.') ));
name = checkValidOperationName(full.substring(full.indexOf('.') + 1, full.length() ));
}
/**
* an Operation with the same name as Action needs to exist. Actions are client side tokens used to identify components and stuff
* @param action
*/
public Operation(final String inter, final String string) throws IllegalArgumentException , NullPointerException {
name = checkValidOperationName(string) ;
interfaceName = checkValidInterfaceName(inter) ;
}
public String checkValidInterfaceName(final String in) throws IllegalArgumentException{
checkNotNull (in) ;
if(in.isEmpty() || in.lastIndexOf(' ') != -1 || in.lastIndexOf('.') != -1)
throw new IllegalArgumentException("Not a valid interface name") ;
else return in ;
}
public String checkValidOperationName(final String op) throws IllegalArgumentException{
checkNotNull(op);
if(op.isEmpty() || op.lastIndexOf(' ') != -1 || op.lastIndexOf('.') != -1)
throw new IllegalArgumentException("Not a valid operation name") ;
else return op ;
}
@SuppressWarnings("unchecked")
@Override
public Operation clone() {
final Operation clone = new Operation(this.getInterfaceName() , this.getName());
try {
if (parameters != null) {
final HashMap<String, Serializable> map = (HashMap<String, Serializable>) parameters.clone();
if (map != null)
clone.setParameters(map);
}
} catch (final Exception e) {
logger.log(Level.WARNING, "Exception in cloning Operation - " + clone.getInterfaceName() + clone.getName() );;
}
return clone;
}
public String getFullyQualifiedName(){
return getInterfaceName() + "." + getName();
}
public String getInterfaceName() {
return interfaceName ;
}
/**
* @return the name
*/
public String getName() {
return name;
}
public HashMap<String, Serializable> getParameters() {
return parameters;
}
public void setInterface(final String in){
interfaceName = in ;
}
public void setName(final String name) {
this.name = name;
}
public void setParameters(final HashMap<String, Serializable> parameters) {
this.parameters = parameters;
}
}
答案 0 :(得分:3)
我自己没有使用过该库,但似乎很清楚为什么你的用法会失败:第一个语句的代码相当于:
Operation op = new Operation("" , ".");
verifyException(op, IllegalArgumentException.class);
考虑一下 - 第一行就是抛出异常,所以第二行无法解决它。
据我所知,verifyException
旨在返回然后你可以调用“坏”操作,返回的假将委托调用并检查异常适当。基本上,我无法看到这对于构造函数调用是如何工作的。
实际上,您链接的文档明确指出:
Catch-exception不提供用于捕获构造函数抛出的异常的API。请改用try-catch-blocks。或者,如果这对您的应用程序有意义,则可以使用构建器模式
(然后有一个例子。)
我强烈期望Java 8将lambda表达式引入Java,单元测试框架将开始使用它们,因此您将能够编写如下内容:
// Not valid yet as far as I know! (But likely to be coming to a test framework
// near you soon...)
assertThrows(IllegalArgumentException.class, () -> new Operation("", "."));
答案 1 :(得分:0)
您不能使用该库来捕获构造函数异常,因为它只在调用现有对象的方法时捕获异常。使用常规Junit来做你想做的事情