Merge pull request #14220 from thibaultfaure/article/BAEL-6482

BAEL-6482 Code for the Matching Null With Mockito article
This commit is contained in:
davidmartinezbarua 2023-06-15 17:05:53 -03:00 committed by GitHub
commit fec5f3af14
3 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package com.baeldung.nullmatcher;
class Helper {
String concat(String a, String b) {
return a + b;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.nullmatcher;
class Main {
Helper helper = new Helper();
String methodUnderTest() {
return helper.concat("Baeldung", null);
}
}

View File

@ -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());
}
}