Trying to Mock an abstract class inside another abstract class

172 views
Skip to first unread message

Thang

unread,
Oct 14, 2016, 4:13:16 PM10/14/16
to mockito
Hi folks, 

I'm trying to mock an inner abstract class contained in an abstract outer class. I have been trying to do something like this:

DoFn<String, ETEvent>.ProcessContext mockContext =
    mock
(DoFn.ProcessContext.class, withSettings().stubOnly().outerInstance(DoFn.class));

However I get the following exception:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class com.google.cloud.dataflow.sdk.transforms.DoFn$ProcessContext.

Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.


Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.91-b14
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 1.8.0_91-b14
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 10.11.6


Underlying exception : java.lang.IllegalStateException: Unknown type variable: OutputT

I imagine this has to do with the outer abstract class, but I was curious if mocking this inner abstract class is possible. Whether or not I should actually be doing this is another question. 

Here's a link to the class definition: ProcessContext

And here's the maven dependency if anyone wants to try this: 

<dependency>
  <groupId>com.google.cloud.dataflow</groupId>
  <artifactId>google-cloud-dataflow-java-sdk-all</artifactId>
  <version>1.8.0</version>
</dependency> 


Thanks for reading,

Thang

Eric Lefevre-Ardant

unread,
Oct 15, 2016, 3:01:40 AM10/15/16
to moc...@googlegroups.com
Hey Thang,

I was not sure what to expect there, so tried it out.

For me, everything works well. I tried both with Mockito 2.0.2-beta and 1.10.19.
I only tried both with my own abstract outer+inner classes and with DoFn.ProcessContext (thanks for the detailed context, BTW).
I can definitely called mock() on them and also call their (mocked) methods.

One difference, though, is that I used Object in place of the ETEvent class that you have:
DoFn<String, Object>.ProcessContext mockContext =
                mock(DoFn.ProcessContext.class, withSettings().stubOnly().outerInstance(DoFn.class));

The error message from Mockito (Unknown type variable: OutputT) makes me think that this is the real issue, although I cannot explain why.
Try with Object, and see how it goes?

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/a356bfed-b0bd-4640-9373-33c5d5b71a58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brice Dutheil

unread,
Oct 17, 2016, 5:10:04 AM10/17/16
to moc...@googlegroups.com
Hey Thang, thanks for reporting!

Thanks Eric for confirming this is a bug in 2.1.0, this looks like to be an issue with bytebuddy. Anyway I will create a bug on mockito to track it.

Next time please don't forget the full stack trace and mockito version ;) 
I reproduced the issue with simpler code to narrow down the issue. Here's the issue : https://github.com/mockito/mockito/issues/699

 

-- Brice

Reply all
Reply to author
Forward
0 new messages