Problem: Conditions within the test method will alter the behavior of the test and its expected output, and would lead to situations where the test fails to detect defects in the production method since test statements were not executed as a condition was not met.
Solution: Instead of using a conditional to control the test execution, we use the proper JUnit API with assumeNotNull().
Result: Before: try (SqlSession session = sqlSessionFactory.openSession()) {...} After: SqlSession session = sqlSessionFactory.openSession(); Assume.assumeNotNull(session);
Comment From: harawata
Hello @eas5 ,
Sorry, I couldn't follow.
As openSession()
never returns null
(it throws exception when something goes wrong), there does not seem to be any difference (except the new code does not close the open session).
Could you explain how it could fail to detect a defect?
Comment From: eas5
Hello @harawata,
I am sorry for the inconvenience. Indeed, although the refactoring does not change the test behavior, the new assumption makes no difference once openSession()
never returns null
.
Thank you for your attention. I am closing the PR now.