--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.
As another responder indicated, you either have to change your CUT (class under test) to accommodate this, or you have to use PowerMockito. Here’s some documentation: http://code.google.com/p/powermock/wiki/MockitoUsage13. It’s definitely more complicated than plain Mockito, but mocking a single static method is about as simple as it gets. It can get quite a bit more complicated. Generally, when you have to use PowerMock, it indicates a design problem in your CUT, implying that you’ve given it multiple (too many) responsibilities. For instance, if you have code that directly accesses a database, put only the direct database access code into a DAO class, with very little conditional logic, and put the logic that depends on what you get from the database (or decides what to put there), in a separate class.
If you’re going to do this, however, A basic outline of what’s required is this:
@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class) // Need this for any classes with static methods that need to be mocked.
public class MyClass {
...
<method signature> {
...
Connection connection = mock(Connection.class);
PowerMockito.mockStatic(DriverManager.class);
PowerMockito.when(DriverManager.getConnection(anyString(), anyString(), anyString()).thenReturn(connection);
--