Prevent NullPointerException on GenericDaoBase#4268
Conversation
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.runners.MockitoJUnitRunner; | ||
| import org.mockito.junit.MockitoJUnitRunner; |
There was a problem hiding this comment.
Previous MockitoJUnitRunner got Deprecated; changed to a non-deprecated one.
| public void prepareTests() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(false); | ||
| Mockito.when(resultSet.getBoolean(1)).thenReturn(false); | ||
| Mockito.when(resultSet.getObject(1)).thenReturn((short) 1); |
There was a problem hiding this comment.
Despite all tests pass, the Process finished with exit code 255 due to unnecessary stubbings.
I fixed this issue by extracting all into a @Before method and tests are looking good.
The output of tests before this PR:
Process finished with exit code 255
org.mockito.exceptions.misusing.UnnecessaryStubbingException:
Unnecessary stubbings detected in test class: GenericDaoBaseTest
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
1. -> at com.cloud.utils.db.GenericDaoBaseTest.getObjectPrimitiveByte(GenericDaoBaseTest.java:144)
2. -> at com.cloud.utils.db.GenericDaoBaseTest.getObjectPrimitiveLong(GenericDaoBaseTest.java:117)
3. -> at com.cloud.utils.db.GenericDaoBaseTest.getObjectPrimitiveInt(GenericDaoBaseTest.java:126)
4. -> at com.cloud.utils.db.GenericDaoBaseTest.getObjectPrimitiveDouble(GenericDaoBaseTest.java:90)
5. -> at com.cloud.utils.db.GenericDaoBaseTest.getObjectPrimitiveFloat(GenericDaoBaseTest.java:81)
6. -> at com.cloud.utils.db.GenericDaoBaseTest.getObjectPrimitiveShort(GenericDaoBaseTest.java:54)
7. -> at com.cloud.utils.db.GenericDaoBaseTest.getObjectPrimitiveBoolean(GenericDaoBaseTest.java:45)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:49)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:54)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
| * @throws EntityExistsException | ||
| */ | ||
| protected static void handleEntityExistsException(SQLException e) throws EntityExistsException { | ||
| boolean isIntegrityConstantViolation = INTEGRITY_CONSTRAINT_VIOLATION.equals(e.getSQLState()); |
There was a problem hiding this comment.
+1 to this pattern to check contant.equals(potentially-nullable-obj)
|
@blueorangutan package |
|
@rhtyd a Jenkins job has been kicked to build packages. I'll keep you posted as I make progress. |
|
Packaging result: ✔centos7 ✔debian. JID-1762 |
|
@blueorangutan test |
|
@rhtyd a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests |
|
Trillian test result (tid-2465)
|
Description
Recently I had a few DB connection/transaction issues on a test environment and one of the log messages was the following
NullPointerException:Looking further we can see that the null pointer was caused due to a null Object calling equals method. A simple fix would be to use the static final not-null Object calling the
Object.equalsmethod.This PR also extracted the duplicated code that could cause
NullPointerExceptionand added unit tests to ensure that noNullPointerExceptionwill be thrown.Types of changes