4

Given class Foo:

public class Foo {
  private Unmarshaller unmarshaller;
  public Foo(Unmarshaller unmarshaller) {}
}

Following test fails:

@Test
public void test() {
  Foo packagingJobSource2 = mock(Foo.class);
}

...with error:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class com.bell.cts.fonse.vod.streaming.services.adapter.cron.avcm.packagingjob.loading.xmlstorage.Foo.

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


Java               : 9
JVM vendor name    : Oracle Corporation
JVM vendor version : 9.0.1+11
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 9.0.1+11
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 10.13.2

I tried using JDK 8 instead of 9 and it works fine. I've no idea why it would fail and neither how I can make this work.

The issue happens specifically when Unmarshaller is a dependency. If you change it for another type like String it works...

Lii
  • 11,553
  • 8
  • 64
  • 88
Jeep87c
  • 1,050
  • 16
  • 36
  • The message says "If you're not sure why you're getting this error, please report to the mailing list". Did you do that? I seriously recommend that you do so. – Dawood ibn Kareem Dec 19 '17 at 19:41
  • Is `Foo` not exported from that module maybe? That would effectively make it a private class. – daniu Dec 19 '17 at 19:42
  • @DawoodibnKareem, just did that! Hopefully someone will be able to help me... – Jeep87c Dec 19 '17 at 19:54
  • Yes, I got the email. I'm sure someone on the list will help you. Not me - I have no idea why this is happening. – Dawood ibn Kareem Dec 19 '17 at 20:01
  • 2
    @Jeep87c Just a side note, could you seek and share the java command formulated to execute your tests? The reason being `Unmarshaller` being a part of the JDK module `@Deprecated(since="9", forRemoval=true) module java.xml.bind`(also being *upgradeable*) in Java9. Also to confirm the [`module java.xml.bind` is not resolved by default.](https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-F640FA9D-FB66-4D85-AD2B-D931174C09A3) – Naman Dec 20 '17 at 08:23

1 Answers1

3

The probable reason for the same is that the module java.xml.bind is not resolved by default. when you compile or run code on the classpath.

If you're relying on the classpath currently and not (planning to) migrating to using the Java Platform Module System (JPMS), you can make use of the (1) option stated in the migration guide and when you run the tests add a VM arg to the execution as:

--add-modules java.xml.bind
Naman
  • 27,789
  • 26
  • 218
  • 353