From 1e4c42c6d11fb9f277433acfcc49e202cac0d6a3 Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Mon, 5 Jun 2023 12:57:33 +0200 Subject: [PATCH] BAEL-6587 Code for the Mockito Exception article --- testing-modules/mockito-2/pom.xml | 6 +-- .../baeldung/wantedbutnotinvocked/Helper.java | 9 +++++ .../baeldung/wantedbutnotinvocked/Main.java | 14 +++++++ .../wantedbutnotinvocked/MainUnitTest.java | 38 +++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Helper.java create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/wantedbutnotinvocked/Main.java create mode 100644 testing-modules/mockito-2/src/test/java/com/baeldung/wantedbutnotinvocked/MainUnitTest.java 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(); + } + +}