BAEL-6482 Code for the Matching Null With Mockito article

This commit is contained in:
thibault.faure 2023-06-06 23:00:55 +02:00
parent fec7001cbd
commit d928abe033
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());
}
}