0

I am currently testing a method updateIssuesTable(). this method is a private void method. Even worse, inside this method is a call to getConnection(), which returns a Connection, and is dependent on several private variables, which are dependent on an intialization method, which is of course dependent on something else.

Long story short, this line of code: Connection conn = getConnection();

I don't want to execute at all, I just need a mocked Connection to continue execution. Belowe is the test method:

public void testUpdateIssuesTable() throws Exception {
        PatchWriterTask task = new PatchWriterTask();

        String sql = "update val_issues set PATCH_CREATION_INFO = ? where VAL_ISSUE_ID = ?";
        when(task1.getConnection()).thenReturn(conn);
        when(conn.prepareStatement(sql)).thenReturn(updateStatement);
        Whitebox.invokeMethod(task, "updateIssuesTable");
    }

The method I am testing is as follows:

    private void updateIssuesTable() throws SQLException {

            PreparedStatement createStatement = null;
            String sql = "update val_issues set PATCH_CREATION_INFO = ? where VAL_ISSUE_ID = ?";
            Connection conn = getConnection();
            createStatement = conn.prepareStatement(sql);
            ...
    }

EDIT: I create a mocked Connection in my test class:

private Connection conn = mock(Connection.class);
DanGordon
  • 671
  • 3
  • 8
  • 26
  • You better create `Connection` object in the Main Class of your program. In every Class' methods() where you wanna use it,`call that method of Main Class which would return an instance of the Connection Object`! – Am_I_Helpful Jun 24 '14 at 21:19
  • This method would still have to establish a connection, by calling a method like `getConnection()`. I am not quite sure what you are suggesting, or why that would help. – DanGordon Jun 24 '14 at 21:23
  • Actually,I didn't understand your statement as to what does `mocking` mean? Sorry,if you feel I am guilty for posting comment without knowing things! But,I thought that you might be requiring only a single Connection object everytime to connect to. Sorry Again for such mistake! – Am_I_Helpful Jun 24 '14 at 21:29
  • possible duplicate of [How do you unit test private methods?](http://stackoverflow.com/questions/250692/how-do-you-unit-test-private-methods) – Raedwald Jun 25 '14 at 12:07
  • Possible duplicate of http://stackoverflow.com/questions/440786/junit-java-testing-non-public-methods – Raedwald Jun 25 '14 at 12:10
  • Possible duplicate of http://stackoverflow.com/questions/34571/whats-the-proper-way-to-test-a-class-with-private-methods-using-junit – Raedwald Jun 25 '14 at 12:12

1 Answers1

0

You should get connection from a connection pool (C3P0 or Tomcat Connection Pool are good candidates), then you can mock the ConnectionPool.getConnection() method to return a connection mock.

Other than that look for @RunWith MockitoJUnitRunner examples to show how you can @InjectMocks into your test classes. No shortcuts here - its easy enough when you know how but will take a bit of time to master.

user1016765
  • 2,935
  • 2
  • 32
  • 48