1

My original CRUD Method generates a Prepared Statement and sets the strings based on the parameters given.

public class StatementUtility {
...
    public static PreparedStatement getFoo(String bar, Connection conn) {
        String query = "SELECT Foo FROM BarTable WHERE Bar = ?";

        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(query);
            pstmt.setString(1, bar);
        }
        catch (SQLException e) {
            ..
        }
        return pstmt;
    }
...
}

In this Statement the Database which I use is set. I created however a TestDB within my MySQL Server where I would like to test a delete Method:

public static String deleteFoo(List<String> input) {
    Connection conn = driver.connectCustomerDB(input);
    try(PreparedStatement pstmt = StatementUtility.getFoo(String someString, conn)) {
    ...
    }
}

Here is my Test so far

@RunWith(PowerMockRunner.class)
@PrepareForTest(StatementUtility.class)
public class DBConnectionBTBAdminTest {


    @Test
    public void deleteTest() {

        List<String> testInput = new ArrayList<>();
        testInput.add("hello");
        testInput.add("World");

        Driver driver = new Driver();
        Connection conn = driver.connectCustomerDB(testInput);

        String query = "FooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBar";

        try {
            //try mocking the Method within
            BDDMockito.given(StatementUtility.getFoo(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), any(Connection.class))).willReturn(conn.prepareStatement(stringBuilder.toString()));
            //call the method I want to test
            SomeClass.deleteCategory(testInput, emptyArray);
            ...
        } catch (SQLException e) {
            ...
        }
    }
}

The error that I get is a Nullpointer Exception in the Method where I create the PreparedStatement originally, but that is not the point as I do not want to get into this Method at all, but stub it.

I also tried using Mockito instead of BDDMockito (see here: https://stackoverflow.com/a/21116014/8830232) and using the real values instead of ArgumentMatchers.*

I also tried some other stuff like mocking the Connection

Currently I am using JUnit@4.12, Mockito@2.13.0, powermock@1.7.1

EDIT: For @glytching answer to work I had to downgrade mockito from 2.x to 1.x. >Dont forget to adjust powermock dependencies in that case

kugtas
  • 113
  • 9

1 Answers1

1

In addition to @PrepareForTest(StatementUtility.class) (which tells PowerMock to prepare this class for testing) you have to enable static mocking for all methods of StatementUtility. You do this by invoking ...

PowerMockito.mockStatic(StatementUtility.class);

... in your test before you attempt to set any expectations on that mock.

For example:

@RunWith(PowerMockRunner.class)
@PrepareForTest(StatementUtility.class)
public class DBConnectionBTBAdminTest {


    @Test
    public void deleteTest() {

        PowerMockito.mockStatic(StatementUtility.class);

        List<String> testInput = new ArrayList<>();
        testInput.add("hello");
        testInput.add("World");

        Driver driver = new Driver();
        Connection conn = driver.connectCustomerDB(testInput);

        String query = "FooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBar";

        try {
           BDDMockito.given(StatementUtility.getFoo(...)).willReturn(...);

            ...
        } catch (SQLException e) {
            ...
        }
    }
}
glytching
  • 44,936
  • 9
  • 114
  • 120
  • Thanks for the quick answer! Your suggestion threw an error. However after using google I downgraded Mockito and it works. However now my Problem is that I have different Connections and therefore I cant execute my query. But you answered my question so thanks!! – kugtas Jan 16 '18 at 15:43