diff --git a/testing-modules/mockito-2/pom.xml b/testing-modules/mockito-2/pom.xml index 25e9fd51a2..fe7ec0fe7a 100644 --- a/testing-modules/mockito-2/pom.xml +++ b/testing-modules/mockito-2/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 mockito-2 diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Helper.java b/testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Helper.java new file mode 100644 index 0000000000..dd8e04065b --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Helper.java @@ -0,0 +1,9 @@ +package com.baeldung.wantedbutnotinvocked; + +class Helper { + + String getBaeldungString() { + return "Baeldung"; + } + +} diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Main.java b/testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Main.java new file mode 100644 index 0000000000..31134000d2 --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Main.java @@ -0,0 +1,14 @@ +package com.baeldung.wantedbutnotinvocked; + +class Main { + + Helper helper = new Helper(); + + String methodUnderTest(int i) { + if (i > 5) { + return helper.getBaeldungString(); + } + return "Hello"; + } + +} diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/wantedbutnotinvocked/MainUnitTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/wantedbutnotinvocked/MainUnitTest.java new file mode 100644 index 0000000000..6927b13a06 --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/wantedbutnotinvocked/MainUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.wantedbutnotinvocked; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +class MainUnitTest { + + @Mock + Helper helper; + + @InjectMocks + Main main = new Main(); + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void givenValueUpperThan5_WhenMethodUnderTest_ThenDelegatesToHelperClass() { + main.methodUnderTest(7); + Mockito.verify(helper) + .getBaeldungString(); + } + + // Uncomment the next line to see the error + // @Test + void givenValueLowerThan5_WhenMethodUnderTest_ThenDelegatesToGetBaeldungString() { + main.methodUnderTest(3); + Mockito.verify(helper) + .getBaeldungString(); + } + +}