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();
}
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.
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.
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.
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.
This is a known issue with Emma.
http://www.eclemma.org/faq.html#trouble05
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With