From d928abe03328a971c6af09db70fa32a8aab89b7c Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Tue, 6 Jun 2023 23:00:55 +0200 Subject: [PATCH] BAEL-6482 Code for the Matching Null With Mockito article --- .../java/com/baeldung/nullmatcher/Helper.java | 9 +++++ .../java/com/baeldung/nullmatcher/Main.java | 11 +++++ .../baeldung/nullmatcher/MainUnitTest.java | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Helper.java create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Main.java create mode 100644 testing-modules/mockito-2/src/test/java/com/baeldung/nullmatcher/MainUnitTest.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Helper.java b/testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Helper.java new file mode 100644 index 0000000000..d15196faa5 --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Helper.java @@ -0,0 +1,9 @@ +package com.baeldung.nullmatcher; + +class Helper { + + String concat(String a, String b) { + return a + b; + } + +} diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Main.java b/testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Main.java new file mode 100644 index 0000000000..e3c73f9d8a --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/nullmatcher/Main.java @@ -0,0 +1,11 @@ +package com.baeldung.nullmatcher; + +class Main { + + Helper helper = new Helper(); + + String methodUnderTest() { + return helper.concat("Baeldung", null); + } + +} diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/nullmatcher/MainUnitTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/nullmatcher/MainUnitTest.java new file mode 100644 index 0000000000..a0245977e0 --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/nullmatcher/MainUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.nullmatcher; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.isNull; + +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; + + @BeforeEach + void openMocks() { + MockitoAnnotations.openMocks(this); + } + + @Test + void whenMethodUnderTest_thenSecondParameterNull() { + main.methodUnderTest(); + Mockito.verify(helper) + .concat("Baeldung", null); + } + + @Test + void whenMethodUnderTest_thenSecondParameterNullWithMatchers() { + main.methodUnderTest(); + Mockito.verify(helper) + .concat(any(), isNull()); + } + +}