static function mocking using Mockito in Guidewire

100 Views Asked by At

I am writing GUnit test cases and attempting to mock a static function in a Class using Mockito 4.11. All the dependent jars are placed in GW plugins/lib folder. I am encountering a ClassNotFoundException as follows. The same set of Mockito jars and dependents are working fine in a Java project using JDK 8. We are using mockito inline mock maker. Guidewire PC version 9.0.5.

    function testSearchById(){
        //GIVEN
        using(var mockedFactory = Mockito.mockStatic(ExternalServiceClient.Type)){
          var party = new Party()
          party.TaxId = "12345"
          party.PartyId = "partyid:12345"
          mockedFactory.when(\ -> ExternalServiceClient.searchById(Mockito.any())).thenReturn(party)
          //WHEN
          var retParty = ExternalServiceClient.searchById("100")
          //THEN
          assertEquals(retParty.PartyId, "partyid:12345")
        }

      }
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
    at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:84)
    at com.sun.proxy.$Proxy65.createStaticMock(Unknown Source)
    at org.mockito.internal.util.MockUtil.createStaticMock(MockUtil.java:202)
    at org.mockito.internal.MockitoCore.mockStatic(MockitoCore.java:134)

Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@347f4f39
at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:56)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:65)

Caused by: java.lang.ClassNotFoundException: org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

I can see the above class in plugins lib. Why is GW class loader struggling to load this class?

1

There are 1 best solutions below

0
On

It is a common issue when using external libraries in Gosu which load classes via the ContextClassLoader. As Martin already commented, xCenter applications use a variety of classloaders and the Thread.currentThread().ContextClassLoader isn't always set correctly, thus external libraries using that classloader tend to blow into your face with a ClassNotFoundException.

This workaround usually works

var originalClassLoader = Thread.currentThread().ContextClassLoader
try {
  Thread.currentThread().ContextClassLoader = this.Class.ClassLoader

// DO STUFF HERE

} finally {
  Thread.currentThread().ContextClassLoader = originalClassLoader
}

Try to minimise the scope of the try block, i.e., messing around with classloaders can have unintended consequences.

I would also like to repeat Martin's remark that Mockito is not fully supported by Guidewire and should be used with caution.

Also please reconsider if the method you're implementing/testing absolutely must be static. Judging by the name only, I see no reason why searchById should be called in a static context on ExternalServiceClient.