-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Prevent NullPointerException on GenericDaoBase #4268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,132 +20,166 @@ | |
| import java.sql.SQLException; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.runners.MockitoJUnitRunner; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previous MockitoJUnitRunner got Deprecated; changed to a non-deprecated one. |
||
|
|
||
| import javax.persistence.EntityExistsException; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class GenericDaoBaseTest { | ||
| @Mock | ||
| ResultSet resultSet; | ||
| @Mock | ||
| SQLException mockedSQLException; | ||
|
|
||
| @Test | ||
| public void getObjectBoolean() throws SQLException { | ||
| private static final String INTEGRITY_CONSTRAINT_VIOLATION = "23000"; | ||
| private static final int DUPLICATE_ENTRY_ERRO_CODE = 1062; | ||
|
|
||
| @Before | ||
| 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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Despite all tests pass, the Process finished with exit code 255 due to unnecessary stubbings. The output of tests before this PR: |
||
| Mockito.when(resultSet.getShort(1)).thenReturn((short) 1); | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(0.1f); | ||
| Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f); | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(0.1d); | ||
| Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d); | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(1l); | ||
| Mockito.when(resultSet.getLong(1)).thenReturn(1l); | ||
| Mockito.when(resultSet.getInt(1)).thenReturn(1); | ||
| Mockito.when(resultSet.getObject(1)).thenReturn((byte) 1); | ||
| Mockito.when(resultSet.getByte(1)).thenReturn((byte) 1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectBoolean() throws SQLException { | ||
| Assert.assertFalse(GenericDaoBase | ||
| .getObject(Boolean.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getBoolean(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectPrimitiveBoolean() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(false); | ||
| Mockito.when(resultSet.getBoolean(1)).thenReturn(false); | ||
| Assert.assertFalse(GenericDaoBase | ||
| .getObject(boolean.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getBoolean(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectPrimitiveShort() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn((short) 1); | ||
| Mockito.when(resultSet.getShort(1)).thenReturn((short) 1); | ||
| Assert.assertEquals(Short.valueOf((short) 1), | ||
| GenericDaoBase.getObject(short.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getShort(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectShort() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn((short) 1); | ||
| Mockito.when(resultSet.getShort(1)).thenReturn((short) 1); | ||
| Assert.assertEquals(Short.valueOf((short) 1), | ||
| GenericDaoBase.getObject(Short.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getShort(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectFloat() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(0.1f); | ||
| Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f); | ||
| Assert.assertEquals(0.1f, | ||
| GenericDaoBase.getObject(Float.class, resultSet, 1), 0.1); | ||
| Mockito.verify(resultSet).getFloat(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectPrimitiveFloat() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(0.1f); | ||
| Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f); | ||
| Assert.assertEquals(0.1f, | ||
| GenericDaoBase.getObject(float.class, resultSet, 1), 0.1); | ||
| Mockito.verify(resultSet).getFloat(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectPrimitiveDouble() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(0.1d); | ||
| Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d); | ||
| Assert.assertEquals(0.1d, | ||
| GenericDaoBase.getObject(double.class, resultSet, 1), 0.1); | ||
| Mockito.verify(resultSet).getDouble(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectDouble() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(0.1d); | ||
| Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d); | ||
| Assert.assertEquals(0.1d, | ||
| GenericDaoBase.getObject(Double.class, resultSet, 1), 0.1); | ||
| Mockito.verify(resultSet).getDouble(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectLong() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(1l); | ||
| Mockito.when(resultSet.getLong(1)).thenReturn(1l); | ||
| Assert.assertEquals((Long) 1l, | ||
| GenericDaoBase.getObject(Long.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getLong(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectPrimitiveLong() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(1l); | ||
| Mockito.when(resultSet.getLong(1)).thenReturn(1l); | ||
| Assert.assertEquals((Long) 1l, | ||
| GenericDaoBase.getObject(long.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getLong(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectPrimitiveInt() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(1l); | ||
| Mockito.when(resultSet.getInt(1)).thenReturn(1); | ||
| Assert.assertEquals((Integer) 1, | ||
| GenericDaoBase.getObject(int.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getInt(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectInteger() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn(1l); | ||
| Mockito.when(resultSet.getInt(1)).thenReturn(1); | ||
| Assert.assertEquals((Integer) 1, | ||
| GenericDaoBase.getObject(Integer.class, resultSet, 1)); | ||
| Mockito.verify(resultSet).getInt(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void getObjectPrimitiveByte() throws SQLException { | ||
| Mockito.when(resultSet.getObject(1)).thenReturn((byte) 1); | ||
| Mockito.when(resultSet.getByte(1)).thenReturn((byte) 1); | ||
| Assert.assertTrue((byte) 1 == GenericDaoBase.getObject(byte.class, | ||
| resultSet, 1)); | ||
| Mockito.verify(resultSet).getByte(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void handleEntityExistsExceptionTestNoMatchForEntityExists() { | ||
| Mockito.when(mockedSQLException.getErrorCode()).thenReturn(123); | ||
| Mockito.when(mockedSQLException.getSQLState()).thenReturn("123"); | ||
| GenericDaoBase.handleEntityExistsException(mockedSQLException); | ||
| } | ||
|
|
||
| @Test | ||
| public void handleEntityExistsExceptionTestIntegrityConstraint() { | ||
| Mockito.when(mockedSQLException.getErrorCode()).thenReturn(123); | ||
| Mockito.when(mockedSQLException.getSQLState()).thenReturn(INTEGRITY_CONSTRAINT_VIOLATION); | ||
| GenericDaoBase.handleEntityExistsException(mockedSQLException); | ||
| } | ||
|
|
||
| @Test | ||
| public void handleEntityExistsExceptionTestIntegrityConstraintNull() { | ||
| Mockito.when(mockedSQLException.getErrorCode()).thenReturn(123); | ||
| Mockito.when(mockedSQLException.getSQLState()).thenReturn(null); | ||
| GenericDaoBase.handleEntityExistsException(mockedSQLException); | ||
| } | ||
|
|
||
| @Test | ||
| public void handleEntityExistsExceptionTestDuplicateEntryErrorCode() { | ||
| Mockito.when(mockedSQLException.getErrorCode()).thenReturn(DUPLICATE_ENTRY_ERRO_CODE); | ||
| Mockito.when(mockedSQLException.getSQLState()).thenReturn("123"); | ||
| GenericDaoBase.handleEntityExistsException(mockedSQLException); | ||
| } | ||
|
|
||
| @Test(expected = EntityExistsException.class) | ||
| public void handleEntityExistsExceptionTestExpectEntityExistsException() { | ||
| Mockito.when(mockedSQLException.getErrorCode()).thenReturn(DUPLICATE_ENTRY_ERRO_CODE); | ||
| Mockito.when(mockedSQLException.getSQLState()).thenReturn(INTEGRITY_CONSTRAINT_VIOLATION); | ||
| GenericDaoBase.handleEntityExistsException(mockedSQLException); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to this pattern to check contant.equals(potentially-nullable-obj)