Verifying Static Method Calls

This section explains how to verify mocked static method calls, and why it's important to include them within unit tests.

Code Example

There may be cases where some code you want to test interacts with an external system, such as a database for example. You wouldn't want to execute the SQL statements within a unit test, especially if the query takes some time. However, we may want to ensure that the mocked DAO is invoked. This can easily be handled with Mockito, but what if the SQL statement is within a static method? That's where verify comes in handy with PowerMock. Below is an example of such a scenario.

UnderTest.java
1|    package main;
2|    
3|    public class UnderTest {
4|        public void foo() {
5|            DAO.executeSQL();
6|        }
7|    }

Method under test foo() that invokes the SQL statement.

DAO.java
1|    package main;
2|    
3|    public class DAO {
4|        public static void executeSQL() {
5|            System.out.println("Execute some SQL.");
6|        }
7|    }

The DAO containing the SQL Statement.

VerifyTest.java
1 |    package test;
2 |
3 |    import main.*;
4 |    import org.junit.Test;
5 |    import org.junit.runner.RunWith;
6 |    import org.powermock.api.mockito.PowerMockito;
7 |    import org.powermock.core.classloader.annotations.PrepareForTest;
8 |    import org.powermock.modules.junit4.PowerMockRunner;
9 |    
10|    @PrepareForTest(DAO.class)
11|    @RunWith(PowerMockRunner.class)
12|    public class VerifyTest {
13|         @Test
14|         public void verifyTest() {
15|             // Object under test
16|             UnderTest obj = new UnderTest();
17|     
18|             // Mock
19|             PowerMockito.mockStatic(DAO.class);
20|     
21|             // Execute method under test
22|             obj.foo();
23|     
24|             // Test
25|             PowerMockito.verifyStatic();
26|             DAO.executeSQL();
27|         }
28|    }

On line 19 we simply mock DAO.execute(), so the SQL statement is not executed. We then call the method under test in line 22, and finally execute the verify on line 25. Notice that there is a method call to executeSQL() immediately after verifyStatic(). This line tells PowerMock to verify a specific method, unlike Mockito, where this information is provided by the mock object as a parameter in Mockito.verify(). The test passes since DAO.executeSQL() is invoked once within obj.foo().

Verifying Multiple Method Calls

Similar to Mockito, PowerMock can also verify that a specific method has been called multiple times. In the previous code example we used PowerMockito.verifyStatic() to verify that we called DAO.executeSQL() exactly once. verifyStatic() with no parameters is actually an alias to verifyStatic(Mockito.times(1)). Mockito.times(int wantedNumberOfInvocations) tells PowerMock how many exact times we want to verfiy that a static method is called, so we could write Mockito.times(5) to verify that a static method was called 5 times for example...

UnderTest.java
1 |    package main;
2 |    
3 |    public class UnderTest {
4 |        public void foo() {
5 |            // Invoke DAO 5 times
6 |            DAO.executeSQL();
7 |            DAO.executeSQL();
8 |            DAO.executeSQL();
9 |            DAO.executeSQL();
10|            DAO.executeSQL();
11|        }
12|    }
VerifyTest.java
1 |    package test;
2 |    
3 |    import main.*;
4 |    import org.junit.Test;
5 |    import org.junit.runner.RunWith;
6 |    import org.mockito.Mockito;
7 |    import org.powermock.api.mockito.PowerMockito;
8 |    import org.powermock.core.classloader.annotations.PrepareForTest;
9 |    import org.powermock.modules.junit4.PowerMockRunner;
10|    
11|    @PrepareForTest(DAO.class)
12|    @RunWith(PowerMockRunner.class)
13|    public class VerifyTest {
14|        @Test
15|        public void verifyTest() {
16|            // Object under test
17|            UnderTest obj = new UnderTest();
18|    
19|            // Mock
20|            PowerMockito.mockStatic(DAO.class);
21|    
22|            // Execute method under test
23|            obj.foo();
24|    
25|            // Test
26|            PowerMockito.verifyStatic(Mockito.times(5));
27|            DAO.executeSQL();
28|        }
29|    }

VerificationMode

Other Mockito methods can be used inside verifyStatic(), such as Mockito.atLeast(int minNumberOfInvocations), and Mockito.atMost(int maxNumberOfInvocations). For more methods, see Mockito JavaDoc method summary, and look at methods that return VerificationMode.

Comments

Popular posts from this blog

Mocking Super Class Method Invocations with PowerMock

Unit Testing a Singleton Using PowerMock