jeudi 3 septembre 2015

Java getDeclaredConstructor with Mockio mock issue

Ok, so I am trying to construct an object using the Class#getDeclaredConstructor method (obviously) and I am having an issue with Mockito CGLIB enhancements. Here is a code snippet:

// Foobar is an interface... 
public Foo(Foobar foobar) {
  this.foobar = foobar;
}

and in another class, i call the following:

List<Class<?>> constructorArgsTypes = new ArrayList<Class<?>>();

for (Object arg : constructorArgs) {
  constructorArgsTypes.add(arg.getClass());
}

Class<?>[] constructorArgsTypesArray = constructorArgsTypes.toArray(new Class<?>[constructorArgs.length]);

return aFactory.getDeclaredConstructor(constructorArgsTypesArray).newInstance(constructorArgs)

When I debug to the last line i get the following exception:

java.lang.NoSuchMethodException: com.foo.bar.baz.Foo$InnerClass.(com.foo.bar.baz.Foobar$$EnhancerByMockitoWithCGLIB$$2e0dcc13)

Debugging, i see the constructorArgsTypesArray contains a single item that is of the following value:

class com.foo.bar.Foobar$$EnhancerByMockitoWithCGLIB$$2e0dcc13

It looks line an anonymous implementation with some CGLib crap... why am i getting this error? I thought that the < ? > was a wildcard of sorts....





Aucun commentaire:

Enregistrer un commentaire