diff --git a/mockito/pom.xml b/mockito/pom.xml index 95b7b46d44..16c7cb0dd8 100644 --- a/mockito/pom.xml +++ b/mockito/pom.xml @@ -1,124 +1,143 @@ - - 4.0.0 - com.baeldung - mockito - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + mockito + 0.1-SNAPSHOT - mockito + mockito - + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + - + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + - - mockito - - - src/main/resources - true - - + - + + mockito + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - - - 4.3.11.Final - 5.1.38 + - - 1.7.13 - 1.1.3 + + + 4.1.5.RELEASE + 3.2.5.RELEASE - - 5.1.3.Final + + 4.3.10.Final + 5.1.35 - - 19.0 - 3.4 + + 1.7.13 + 1.1.3 - - 1.3 - 4.12 - 1.10.19 + + 5.1.3.Final - 4.4.1 - 4.5 + + 19.0 + 3.4 - 2.9.0 + + 1.3 + 4.12 + 1.10.19 + 1.6.4 - - 3.5.1 - 2.6 - 2.19.1 - 2.7 - 1.4.18 + 4.4.1 + 4.5 - + 2.4.1 + + + 3.3 + 2.6 + 2.18.1 + 2.7 + 1.4.14 + + \ No newline at end of file diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorForPartialMocking.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorForPartialMocking.java new file mode 100644 index 0000000000..f6ae4d80ce --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorForPartialMocking.java @@ -0,0 +1,19 @@ +package com.baeldung.powermockito.introduction; + +public class CollaboratorForPartialMocking { + public static String staticMethod() { + return "Hello Baeldung!"; + } + + public final String finalMethod() { + return "Hello Baeldung!"; + } + + private String privateMethod() { + return "Hello Baeldung!"; + } + + public String privateMethodCaller() { + return privateMethod() + " Welcome to the Java world."; + } +} \ No newline at end of file diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithFinalMethods.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithFinalMethods.java new file mode 100644 index 0000000000..5c5eba46b7 --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithFinalMethods.java @@ -0,0 +1,7 @@ +package com.baeldung.powermockito.introduction; + +public class CollaboratorWithFinalMethods { + public final String helloMethod() { + return "Hello World!"; + } +} diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithStaticMethods.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithStaticMethods.java new file mode 100644 index 0000000000..071fcc6487 --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithStaticMethods.java @@ -0,0 +1,15 @@ +package com.baeldung.powermockito.introduction; + +public class CollaboratorWithStaticMethods { + public static String firstMethod(String name) { + return "Hello " + name + " !"; + } + + public static String secondMethod() { + return "Hello no one!"; + } + + public static String thirdMethod() { + return "Hello no one again!"; + } +} \ No newline at end of file diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/PowerMockitoTest.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/PowerMockitoTest.java new file mode 100644 index 0000000000..2edb76a44e --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/PowerMockitoTest.java @@ -0,0 +1,77 @@ +package com.baeldung.powermockito.introduction; + +import static org.junit.Assert.assertEquals; +import static org.powermock.api.mockito.PowerMockito.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(fullyQualifiedNames = "com.baeldung.powermockito.introduction.*") +public class PowerMockitoTest { + @Test + public void givenFinalMethods_whenUsingPowerMockito_thenCorrect() throws Exception { + CollaboratorWithFinalMethods mock = mock(CollaboratorWithFinalMethods.class); + whenNew(CollaboratorWithFinalMethods.class).withNoArguments().thenReturn(mock); + + CollaboratorWithFinalMethods collaborator = new CollaboratorWithFinalMethods(); + verifyNew(CollaboratorWithFinalMethods.class).withNoArguments(); + + when(collaborator.helloMethod()).thenReturn("Hello Baeldung!"); + String welcome = collaborator.helloMethod(); + Mockito.verify(collaborator).helloMethod(); + assertEquals("Hello Baeldung!", welcome); + } + + @Test(expected = RuntimeException.class) + public void givenStaticMethods_whenUsingPowerMockito_thenCorrect() { + mockStatic(CollaboratorWithStaticMethods.class); + + when(CollaboratorWithStaticMethods.firstMethod(Mockito.anyString())).thenReturn("Hello Baeldung!"); + when(CollaboratorWithStaticMethods.secondMethod()).thenReturn("Nothing special"); + doThrow(new RuntimeException()).when(CollaboratorWithStaticMethods.class); + CollaboratorWithStaticMethods.thirdMethod(); + + String firstWelcome = CollaboratorWithStaticMethods.firstMethod("Whoever"); + String secondWelcome = CollaboratorWithStaticMethods.firstMethod("Whatever"); + + assertEquals("Hello Baeldung!", firstWelcome); + assertEquals("Hello Baeldung!", secondWelcome); + + verifyStatic(Mockito.times(2)); + CollaboratorWithStaticMethods.firstMethod(Mockito.anyString()); + + verifyStatic(Mockito.never()); + CollaboratorWithStaticMethods.secondMethod(); + + CollaboratorWithStaticMethods.thirdMethod(); + } + + @Test + public void givenPartialMocking_whenUsingPowerMockito_thenCorrect() throws Exception { + String returnValue; + + spy(CollaboratorForPartialMocking.class); + when(CollaboratorForPartialMocking.staticMethod()).thenReturn("I am a static mock method."); + returnValue = CollaboratorForPartialMocking.staticMethod(); + verifyStatic(); + CollaboratorForPartialMocking.staticMethod(); + assertEquals("I am a static mock method.", returnValue); + + CollaboratorForPartialMocking collaborator = new CollaboratorForPartialMocking(); + CollaboratorForPartialMocking mock = spy(collaborator); + + when(mock.finalMethod()).thenReturn("I am a final mock method."); + returnValue = mock.finalMethod(); + Mockito.verify(mock).finalMethod(); + assertEquals("I am a final mock method.", returnValue); + + when(mock, "privateMethod").thenReturn("I am a private mock method."); + returnValue = mock.privateMethodCaller(); + verifyPrivate(mock).invoke("privateMethod"); + assertEquals("I am a private mock method. Welcome to the Java world.", returnValue); + } +} \ No newline at end of file