Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A successful JUnit coverage test doesn't turn green on methods that test exceptions

I have a small JUnit test that expects an exception to be thrown. The method throws the exception as expected but when I'm running a "Coverage As" run in Eclipse SpringSource ToolSuite, the two lines in the test method stay red instead of turning green. This test passes--no issues there.

The mockPropertyReader is an empty Properties file reader class. I'm simply testing for the exception if the property is malformed or doesn't exist.

I'm just thinking the method lines should be highlighted to green. I like lots of green.

@Test(expected = PropertyNotFoundException.class)
public final void testGetMySQLNotFound() throws PropertyNotFoundException {
    subject.setSqlProps(mockPropertyReader);
    subject.getMySQL();
}
like image 485
Davidson Avatar asked Dec 06 '12 16:12

Davidson


People also ask

What is meant by code coverage when working with JUnit tests?

Code coverage is a metric we use to quantify how much of our code based is being reached by our unit tests. Creating unit tests and not knowing for sure if they cover at least the most important scenarios, paths, and edge cases seems to be a profitless idea.

What are exceptions in JUnit?

JUnit provides an option of tracing the exception handling of code. You can test whether the code throws a desired exception or not. The expected parameter is used along with @Test annotation.

What does coverage mean in JUnit?

Eclipse can check your code coverage when it runs your JUnit testing class. This means that it can show you what statements were executed in at least one test case and what ones weren't. For an if-statement, it will tell you whether there was a test case for the condition to be false and another for it to be true.

Does JUnit give code coverage?

With JUnit 3 and JUnit 4 you will only get code coverage result for the whole test suite, not individual test cases. With JUnit 4 with Enhanced Code Coverage you will see results under JUnit Code Coverage Workspace Results for each JUnit test case within a JUnit test suite.


1 Answers

This is a known issue with Emma.

http://www.eclemma.org/faq.html#trouble05

like image 177
artbristol Avatar answered Sep 23 '22 23:09

artbristol