Hello Vincent,
You can't use weld-junit together with @QuarkusTest. In fact, these are
two different tools, each has a different goal. weld-junit is a tool for
CDI component testing whereas @QuarkusTest can be used to test your
application as a whole.
A basic mock support in @QuarkusTest will be added to the next release
(0.12.0):
https://github.com/quarkusio/quarkus/blob/master/docs/src/main/asciidoc/getting-started-testing.adoc#user-content-mock-support
Also note that Quarkus DI is based on CDI but it's not a full
implementation - not every feature that can be tested with weld-junit
will work in Quarkus.
Martin
Dne 12. 03. 19 v 21:50 V. Sevel napsal(a):
> hello,
> I have been trying to write some tests where one of the cdi bean gets
> mocked.
> I have used weld-junit5, which seems to work to some level.
>
> here is my test:
>
> @QuarkusTest
> @EnableAutoWeld
> public class MyTest {
>
> @Inject
> GreetingServiceservice;
>
> @Produces
> @OverrideBean
> CoucouServicefakeCoucou =new CoucouMock();
>
> @Test
> public void mytest() {
> Assertions.assertEquals("hello mock",service.greeting());
> }
> }
>
>
> with src/main/java:
>
>
> @ApplicationScoped
> public class GreetingService {
>
> @Inject
> CoucouServicecoucou;
>
> public String greeting(String name) {
> return "hello " + name;
> }
>
> public String greeting() {
> return "hello " +coucou.getName();
> }
> }
>
>
> @ApplicationScoped
> public class CounterService {
>
> AtomicIntegercounter =new AtomicInteger();
>
> public int next() {
> return counter.incrementAndGet();
> }
> }
>
>
> and
>
>
> @ApplicationScoped
> public class CoucouService {
>
> @Inject
> CounterServicecounterService;
>
> public String getName() {
> return "coucou" +counterService.next();
> }
> }
>
>
> in src/test/java I have added for the test:
>
>
> @ApplicationScoped
> public class CoucouMockextends CoucouService {
>
> @Override
> public String getName() {
> return super.getName() + "mock";
> }
> }
>
>
> if I run test test, I get:
>
>
>
> java.lang.NullPointerException
> at org.acme.quickstart.CoucouService.getName(CoucouService.java:19)
> at org.acme.quickstart.CoucouMock.getName(CoucouMock.java:17)
> at org.acme.quickstart.GreetingService.greeting(GreetingService.java:23)
> at org.acme.quickstart.GreetingService$Proxy$_$$_WeldClientProxy.greeting(Unknown Source)
> at org.acme.quickstart.MyTest.mytest(MyTest.java:31)
>
>
> this means that the @OverrideBean worked as expected, but the inherited CoucouService.counterService is null because obviously I am instantiating directly CoucouServicefakeCoucou = new CoucouMock(), and counterService from the superclass
> does not get instantiated.;
>
>
> my goal here was simply to be able to reimplement some of the methods
> from the original CoucouService in a subclass (ie: CoucouMock) that would be used everywherewe needed the original CoucouService, but still benefiting from the
> default behavior of CoucouService (which gets injected the CounterService)
>
>
> what is the proper way of adressing this kind of use case? mockito?
>
>
> thanks,
>
> vincent
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Quarkus Development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
quarkus-dev...@googlegroups.com
> <mailto:
quarkus-dev...@googlegroups.com>.
> Visit this group at
https://groups.google.com/group/quarkus-dev.
> For more options, visit
https://groups.google.com/d/optout.