Merge pull request #15710 from thibaultfaure/articles/BAEL-7399-Injecting-@Captor-in-Junit-Method-Parameters
BAEL-7399 Code for the Injecting @Mock and @Captor in JUnit5 Method P…
This commit is contained in:
commit
a1353ca196
|
@ -18,10 +18,17 @@
|
||||||
<version>${mockito-inline.version}</version>
|
<version>${mockito-inline.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-junit-jupiter</artifactId>
|
||||||
|
<version>${mockito.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<mockito-inline.version>4.8.1</mockito-inline.version>
|
<mockito-inline.version>4.8.1</mockito-inline.version>
|
||||||
|
<mockito.version>5.9.0</mockito.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.injection;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class ConstructorInjectionUnitTest {
|
||||||
|
|
||||||
|
Function<String, String> function;
|
||||||
|
|
||||||
|
ArgumentCaptor<String> captor;
|
||||||
|
|
||||||
|
public ConstructorInjectionUnitTest(@Mock Function<String, String> function, @Captor ArgumentCaptor<String> captor) {
|
||||||
|
this.function = function;
|
||||||
|
this.captor = captor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenInjectedViaArgumentParameters_thenSetupCorrectly() {
|
||||||
|
when(function.apply("bael")).thenReturn("dung");
|
||||||
|
assertEquals("dung", function.apply("bael"));
|
||||||
|
verify(function).apply(captor.capture());
|
||||||
|
assertEquals("bael", captor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung.injection;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.RepeatedTest;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class MethodParameterInjectionUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenMockInjectedViaArgumentParameters_thenSetupCorrectly(@Mock Function<String, String> mockFunction) {
|
||||||
|
when(mockFunction.apply("bael")).thenReturn("dung");
|
||||||
|
assertEquals("dung", mockFunction.apply("bael"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenArgumentCaptorInjectedViaArgumentParameters_thenSetupCorrectly(@Mock Function<String, String> mockFunction, @Captor ArgumentCaptor<String> captor) {
|
||||||
|
mockFunction.apply("baeldung");
|
||||||
|
verify(mockFunction).apply(captor.capture());
|
||||||
|
assertEquals("baeldung", captor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@RepeatedTest(2)
|
||||||
|
void whenInjectedInRepeatedTest_thenSetupCorrectly(@Mock Function<String, String> mockFunction, @Captor ArgumentCaptor<String> captor) {
|
||||||
|
mockFunction.apply("baeldung");
|
||||||
|
verify(mockFunction).apply(captor.capture());
|
||||||
|
assertEquals("baeldung", captor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"", "bael", "dung"})
|
||||||
|
void whenInjectedInParameterizedTest_thenSetupCorrectly(String input, @Mock Function<String, String> mockFunction, @Captor ArgumentCaptor<String> captor) {
|
||||||
|
when(mockFunction.apply(input)).thenReturn("baeldung");
|
||||||
|
assertEquals("baeldung", mockFunction.apply(input));
|
||||||
|
verify(mockFunction).apply(captor.capture());
|
||||||
|
assertEquals(input, captor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue