mock cdi bean

478 views
Skip to first unread message

V. Sevel

unread,
Mar 12, 2019, 4:50:15 PM3/12/19
to Quarkus Development mailing list
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
GreetingService service;

@Produces
@OverrideBean
CoucouService fakeCoucou = new CoucouMock();

@Test
public void mytest() {
Assertions.assertEquals("hello mock", service.greeting());
}
}

with src/main/java:

@ApplicationScoped
public class GreetingService {

@Inject
CoucouService coucou;

public String greeting(String name) {
return "hello " + name;
}

public String greeting() {
return "hello " + coucou.getName();
}
}

@ApplicationScoped
public class CounterService {

AtomicInteger counter = new AtomicInteger();

public int next() {
return counter.incrementAndGet();
}
}

and

@ApplicationScoped
public class CoucouService {

@Inject
CounterService counterService;

public String getName() {
return "coucou" + counterService.next();
}
}

in src/test/java I have added for the test:

@ApplicationScoped
public class CoucouMock extends 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 CoucouService fakeCoucou = 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 everywhere we 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



Martin Kouba

unread,
Mar 14, 2019, 11:33:15 AM3/14/19
to vvs...@gmail.com, Quarkus Development mailing list
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.

V. Sevel

unread,
Mar 14, 2019, 5:23:57 PM3/14/19
to Quarkus Development mailing list
will there be an opportunity to change the alternative for a given service from test to test?
from the doc it does not seem so. 

Stuart Douglas

unread,
Mar 17, 2019, 8:28:12 PM3/17/19
to Quarkus Development mailing list
This is planned, but has not been implemented yet.

Stuart

Jeremy Davis

unread,
Dec 14, 2019, 2:24:05 PM12/14/19
to Quarkus Development mailing list
Has this made it into a release?

Georgios Andrianakis

unread,
Dec 14, 2019, 2:33:59 PM12/14/19
to Jeremy Davis, Quarkus Development mailing list
Nope, it hasn't.

> Visit this group at https://groups.google.com/group/quarkus-dev.
> For more options, visit https://groups.google.com/d/optout.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/b09d67ea-5477-4076-aef8-ea1aa8bf99b6%40googlegroups.com.

John Clingan

unread,
Dec 17, 2019, 10:50:04 AM12/17/19
to Quarkus Development mailing list
FYI, the recommended approach is to use a CDI @Alternative

V. Sevel

unread,
May 2, 2020, 6:14:51 AM5/2/20
to Quarkus Development mailing list
it sounds like the new QuarkusMock is doing everything we need
Reply all
Reply to author
Forward
0 new messages