Commits

Micha Kops  committed 3722e19

Examples for mocking of static classes with Mockito and PowerMock added

  • Participants
  • Parent commits 0a6fa6f

Comments (0)

Files changed (2)

File mockito-tutorial/pom.xml

 			<version>4.8.2</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.powermock</groupId>
+			<artifactId>powermock-mockito-release-full</artifactId>
+			<version>1.4.8</version>
+			<type>pom</type>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 </project>

File mockito-tutorial/src/test/java/com/hascode/tutorial/StaticClassExample.java

+package com.hascode.tutorial;
+
+import static org.mockito.Mockito.when;
+import junit.framework.Assert;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class) @PrepareForTest(Math.class) public class StaticClassExample {
+
+	@Test public void testMockStatic() {
+		PowerMockito.mockStatic(Math.class);
+		when(Math.max(1, 5)).thenReturn(1); // this is soo wrong ;)
+		Assert.assertEquals(1, Math.max(1, 5));
+	}
+}