简体   繁体   English

在JUnit测试中强制执行JAXBException

[英]Forcing a JAXBException in JUnit test

I'm trying to make a JUnit exception test for an unmarshalling method. 我正在尝试对解组方法进行JUnit异常测试。

This is my unmarshalling method (note: I am returning a String because of normal testing unmarshalling testing with expected string). 这是我的解组方法(注意:由于正常的测试,我将返回一个String与预期的字符串解组测试)。

public String UnMarshalling(String FILE)
{
    ArrayList<Player> playerList = new ArrayList<Player>();
    try {
        JAXBContext context = JAXBContext.newInstance(Match.class);
        Unmarshaller um = context.createUnmarshaller();
        Match Match2 = (Match) um.unmarshal(new InputStreamReader(new FileInputStream(FILE), StandardCharsets.UTF_8));
        playerList = Match2.playerList;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }

    return playerList.toString();
}

Here are the tests for this method. 这是此方法的测试。

@Test
public void unMarshallingTest() {
    assertTrue(marshalling.UnMarshalling(matchxml).contains("Petras"));
}

@Test(expected=JAXBException.class)
public void marshallingTestException()
{
        marshalling.UnMarshalling(matchbrokenxml);    
}

What I'm trying to achieve is sending broken xml so for example with a wrong version of xml and getting JAXBException . 我要实现的目标是发送损坏的xml,例如,使用错误版本的xml并获取JAXBException

So far I have scouted internet for an example but found nothing. 到目前为止,我以互联网搜索为例,但没有发现任何结果。 Any suggestions on how to achieve this? 关于如何实现这一目标的任何建议?

You are catching and swallowing the exception ie UnMarshalling() never throws a JAXBException (or any subclass of that). 您正在捕获并吞没异常,即UnMarshalling()永远不会抛出JAXBException (或其任何子类)。

This will work: 这将起作用:

public String UnMarshalling(String FILE) throws JAXBException {
    ArrayList<Player> playerList = new ArrayList<Player>();
    try {
        JAXBContext context = JAXBContext.newInstance(Match.class);
        Unmarshaller um = context.createUnmarshaller();
        Match Match2 = (Match) um.unmarshal(new InputStreamReader(new FileInputStream(FILE), StandardCharsets.UTF_8));
        playerList = Match2.playerList;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } 

    return playerList.toString();
}


@Test(expected=UnmarshalException.class)
public void marshallingTestException() {

    marshalling.UnMarshalling(matchbrokenxml);

}

The main change here is the removal of the catch clause for JAXBException and adding throws JAXBException to the method declaration. 这里的主要更改是删除JAXBException的catch子句,并向方法声明中添加throws JAXBException

Your test strongly suggests that JAXBException is part of the public API for this method, in which case declaring the method to throw JAXBEception makes sense. 您的测试强烈建议JAXBException是此方法的公共API的一部分,在这种情况下,声明该方法以抛出JAXBEception是有意义的。 On the other hand if you really do not want or need JAXBException in the method signature then your test case is either redundant or is addressing the wrong exception type. 另一方面,如果您真的不希望或不需要方法签名中的JAXBException ,则您的测试用例要么是多余的,要么是解决了错误的异常类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM