-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35606 from manovotn/issue35581
Mockito testing - manually activate req. context when creating Mockito spies
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/RequestScopedSpyTest.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,65 @@ | ||
package io.quarkus.it.mockbean; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.mockito.InjectSpy; | ||
|
||
/** | ||
* Tests that Mockito spies can be RequestScoped - this case is different from app scoped/singletons because by the time | ||
* we create spies (right after creating test instance), we need to manually activate req. context for this creation. | ||
*/ | ||
@QuarkusTest | ||
public class RequestScopedSpyTest { | ||
|
||
@InjectSpy | ||
private RequestBean spiedBean; | ||
|
||
@Inject | ||
private SomeOtherBean injectedBean; | ||
|
||
@Test | ||
void verifySpyWorks() { | ||
// Executes gracefully | ||
assertNotNull(spiedBean); | ||
injectedBean.pong(); | ||
Mockito.verify(spiedBean, Mockito.times(1)).ping(); | ||
} | ||
|
||
@Nested | ||
class NestedTest { | ||
@Test | ||
void verifyNestedSpyWorks() { | ||
assertNotNull(spiedBean); | ||
injectedBean.pong(); | ||
Mockito.verify(spiedBean, Mockito.times(1)).ping(); | ||
} | ||
} | ||
|
||
@RequestScoped | ||
public static class RequestBean { | ||
|
||
public void ping() { | ||
|
||
} | ||
} | ||
|
||
@ApplicationScoped | ||
public static class SomeOtherBean { | ||
|
||
@Inject | ||
RequestBean bean; | ||
|
||
public void pong() { | ||
bean.ping(); | ||
} | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
io.quarkus.test.junit.mockito.internal.ResetOuterMockitoMocksCallback | ||
io.quarkus.test.junit.mockito.internal.CreateMockitoSpiesCallback |