trouble mocking one method

158 views
Skip to first unread message

Sébastien Dionne

unread,
Feb 22, 2021, 11:06:36 AM2/22/21
to Quarkus Development mailing list
In a junit, I want to mock one method of a instance.  I have a method (  retrieveScannersFromMachineConfig  (); ) that will call a system that is not available and I want to mock the result.  I want the others methods to works normaly.

I try to use spy and mock the method.. but in always go into the real method.. not the mocked one.  I don't know what I did wrong.

The method is this: retrieveScannersFromMachineConfig(NodeDTO node)

I have this

@Inject
ScannerCodec scannerCodec;

@Test
public void testScannersAsync() throws Exception {
...

//ScannerCodec scannerCodecMock = Mockito.mock(ScannerCodec.class, CALLS_REAL_METHODS);
ScannerCodec scannerCodecMock = Mockito.spy(scannerCodec);

Mockito.doReturn(scannerList).when(scannerCodecMock).retrieveScannersFromMachineConfig(any(NodeDTO.class));
//Mockito.when(scannerCodecMock.retrieveScannersFromMachineConfig(machineCodecNodeInTwin)).thenReturn(scannerList);
//Mockito.when(scannerCodecMock.refreshScanners()).thenCallRealMethod();

// will try multiple times to retrieve the scanners
scannerCodecMock.refreshScanners();

I try differents things.. but always return the same results.. except when I use Mock on a class.. I'll obtain NPE for the variable into ScannerCodec (because the are injected)

Georgios Andrianakis

unread,
Feb 22, 2021, 11:50:42 AM2/22/21
to Sébastien Dionne, Quarkus Development mailing list
Did you try:

Mockito.when(scannerCodecMock.retrieveScannersFromMachineConfig(any(NodeDTO.class)).thenReturn(scannerList);

?

--
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/676b38aa-4317-4037-8352-ceade7ecb289n%40googlegroups.com.

Sébastien Dionne

unread,
Feb 22, 2021, 12:13:05 PM2/22/21
to Quarkus Development mailing list
yes.  same result.  

if I put a breakpoint in the junit test I obtain this

com.comact.iep.codec.service.ScannerCodec@7320bccc 
scannerCodecMock = {ScannerCodec_ClientProxy$MockitoMock$1546714229@15622} "com.comact.iep.codec.service.ScannerCodec@7320bccc"
 mockitoInterceptor = {MockMethodInterceptor@15662} 
  handler = {InvocationNotifierHandler@15666} 
  mockCreationSettings = {CreationSettings@15667} 
   typeToMock = {Class@10846} "class com.comact.iep.codec.service.ScannerCodec_ClientProxy"
   extraInterfaces = {HashSet@15680}  size = 0
   name = null
   spiedInstance = {ScannerCodec_ClientProxy@15617} "com.comact.iep.codec.service.ScannerCodec@7320bccc"
..

we see this : ScannerCodec@7320bccc

but when I'm in the method and I check at the variables, I see this  (The class adress is not the same.)

this = {ScannerCodec@15595} 
 twinRestConnector = {TwinRestConnector_27306872b9b9d6a5cde2e5a56fad18628bb23077_Synthetic_ClientProxy@15597} "RESTEASY004635: Resteasy Client Proxy for : com.comact.iep.connector.TwinRestConnector"
 cmsClientRestConnector = {CmsClientRestConnector_60f95cf0d8aaaa2d85e93c531c682723476b56ec_Synthetic_ClientProxy@15596} "RESTEASY004635: Resteasy Client Proxy for : com.comact.iep.connector.CmsClientRestConnector"
 controller = {ScannerCodecController_ClientProxy@15598} "com.comact.iep.codec.controller.ScannerCodecController@26401eda"
 chartHelper = {ChartHelper_ClientProxy@15599} "com.comact.iep.codec.util.ChartHelper@118cbb26"
 AbstractCodec.twinRestConnector = {TwinRestConnector_27306872b9b9d6a5cde2e5a56fad18628bb23077_Synthetic_ClientProxy@15597} "RESTEASY004635: Resteasy Client Proxy for : com.comact.iep.connector.TwinRestConnector"
 kubernetesApiRestConnector = {KubernetesApiRestConnector_f22da27362baf853b2c5fd216af58a517f31ec01_Synthetic_ClientProxy@15600} "RESTEASY004635: Resteasy Client Proxy for : com.comact.iep.connector.KubernetesApiRestConnector"
 codecValidator = {CodecValidator_ClientProxy@15601} "com.comact.iep.codec.validation.CodecValidator@3299e315"
 mainCodecController = {MainCodecController_Subclass@15602} 


not sure that I missed something so far.  I think the next step is to trace it in Mockito ?


Sébastien Dionne

unread,
Feb 22, 2021, 12:53:19 PM2/22/21
to Quarkus Development mailing list
I traced into Mockito.

if I use this

ScannerCodec scannerCodecMock = Mockito.spy(scannerCodec);
Mockito.doReturn(scannerList).when(scannerCodecMock).retrieveScannersFromMachineConfig(any(NodeDTO.class));
// will try multiple times to retrieve the scanners
scannerCodecMock.refreshScanners();

Mockito will check if the method : refreshScanners() is mocked.  if it's not the case.. it will use the real implementation.  But in the real refreshScanners() it will call retrieveScannersFromMachineConfig()..  but mockito is not called.

at least, I know what happen.  Still need to figure out what I should do to mock that usecase

Reply all
Reply to author
Forward
0 new messages