forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QuarkusComponentTest: fix
@InjectMock
inconsistency
- take into consideration method params with `@InjectMock` when marking beans as unremovable - document that `@InjectMock` is not intended as a universal replacement for the functionality provided by the Mockito JUnit extension. - also skip param injection for params annotated with `@org.mockito.Mock` so that `@SkipInject` is not needed. - fixes quarkusio#41224
- Loading branch information
Showing
4 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...unit5-component/src/test/java/io/quarkus/test/component/mockito/MockitoExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.quarkus.test.component.mockito; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.component.QuarkusComponentTest; | ||
|
||
@QuarkusComponentTest | ||
public class MockitoExtensionTest { | ||
|
||
// Bar - component under test, real bean | ||
// Baz - mock of the synthetic bean registered to satisfy Bar#baz | ||
// Foo - plain Mockito mock | ||
@ExtendWith(MockitoExtension.class) | ||
@Test | ||
public void testInjectMock(Bar bar, @InjectMock Baz baz, @Mock Foo foo) { | ||
Mockito.when(foo.pong()).thenReturn(false); | ||
Mockito.when(baz.ping()).thenReturn(foo); | ||
assertFalse(bar.ping().pong()); | ||
} | ||
|
||
@Singleton | ||
public static class Bar { | ||
|
||
@Inject | ||
Baz baz; | ||
|
||
Foo ping() { | ||
return baz.ping(); | ||
} | ||
|
||
} | ||
|
||
public static class Baz { | ||
|
||
Foo ping() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
public static class Foo { | ||
|
||
boolean pong() { | ||
return true; | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...omponent/src/test/java/io/quarkus/test/component/paraminject/ParameterInjectMockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.quarkus.test.component.paraminject; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.component.QuarkusComponentTest; | ||
|
||
@QuarkusComponentTest | ||
public class ParameterInjectMockTest { | ||
|
||
// Foo is mocked even if it's not a dependency of a tested component | ||
@Test | ||
public void testInjectMock(@InjectMock MyFoo foo) { | ||
Mockito.when(foo.ping()).thenReturn(false); | ||
assertFalse(foo.ping()); | ||
} | ||
|
||
public static class MyFoo { | ||
|
||
boolean ping() { | ||
return true; | ||
} | ||
} | ||
|
||
} |