I’m converting java jUnit tests to scala. As scala does not use checked exceptions this java code :
@Test
public void testException throws Exception = {
throw new Exception
}
can be re-written in scala as :
@Test def testException = {
throw new Exception
}
The test written in java will fail on line
throw new Exception
but the test written in scala will pass even though it also throws an Exception
I could re-write the scala test to something like below so if an exception is thrown I explicitly force the test to fail by calling junits fail method :
@Test def testException = {
try {
} catch {
case e: Exception => {
fail("exception thrown "+e.getMessage());
}
}
}
Should I be writing the tests like this ? In other words for any java tests that throw Exceptions should I be re-writing the scala version of the test to explicitly catch and then fail to mimic how the java version of the test behaves ?
No, you definitely shouldn’t. The only thing it does is to make your tests more verbose and have worse stack traces and assertion messages.
No, any JUnit test which throws exceptions will fail, it doesn’t matter what language it’s written in.