eclemma 1 of 2 branch not covered in Junit

北城余情 提交于 2019-12-13 00:56:17

问题


JUnit file

public class SomeClassTest {
    ...
    @Test
    public void testSomeContextFailure() throws Exception {
        SomeClass sc = new SomeClass();
        try {
            sc.SomeContext(null,null);
            fail("IllegalArg expected");
        } catch (IllegalArgumentException e) {}
    }

    @Test
    public void testSomeContextSuccess() throws Exception {
        SomeClass sc = new SomeClass();
        SomeContext in = new SomeContext();
        in.setName("something");
        in.setId("5");
        in.setPoints(SomePoint.X);
        try {
            assertNotNull(sc.SomeContext(in,null));
        } catch (Exception e) {}
    }
}

Java File

public class SomeClassTest {
    @Autowired(required = true)
    private InsuredDAO insuredDAO;

    @Override
    public context SomeContext(context c, unused u) throws Exception {
        if(c == null)
            throw new IllegalArgumentException();

        insuredDAO.increaseValue(c);
        if(c.getPoints() != null) {
            ...do something
        }
        return c;
}

In java file if(c == null) was highlighted yellow with message saying 1 of 2 branches not covered.

throw new IllegalArgumentException();

highlighted green

insuredDAO.increaseValue(c); 

Everything on and below this line is red

What am I missing? (JUnit test was passed on both but why it isn't covered)?


回答1:


Might be a bit too late, but if someone else comes across this...
First:
You have two tests of which one of them fails. As the only test, that went through, tested the "null" value, eclemma marks the if statement as "partially covered". -> null check was tested, but with the given Object, it was not.
It doesn't work that way like a debugger "the test ran up until this line, then it failed, but everything it did until this stage will be analyzed". eclemma just analyzes fully run (and succeeded) tests.
Second:
In your class you have insuredDAO.increaseValue(c);, which is autowired. In my tests I have to mock this Object, otherwise it will fail with a NullPointerException at that line because autowiring is not done in junit tests, therefore insuredDAO is null and the method call will throw a NullPointerException.
Should have popped up in your IDE, though ;)



来源:https://stackoverflow.com/questions/30226789/eclemma-1-of-2-branch-not-covered-in-junit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!