I use following test to test my utitiliesclass, I use mockito for the sql connection.
@Mock
public Connection connectionMock;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testResource(){
String sql = Utilities.resourceToString("testSql.sql");
try {
Mockito.when(connectionMock.createStatement().executeQuery(sql)).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock) throws Throwable {
return "X";
}
});
I get a nullpointer on the line Mockito.when, what is wrong?
You need another mock...
...will return null unless you set up an expectation for it.
For example, add...
Update
To answer the comment below, you should be returning a result set, not a string. For example...
Update #2
Removed stuff that was wrong
Actually, you can have a better usage about Mockito: