jeudi 28 avril 2016

Mocking a ServletRequest throws exception

To improve my code coverage I mock my methods containing servlet requests and responses, so I create a Unit test with following structure:

public class AccessFilterTest {
    private AccessFilter accessFilter;

    @Mock
    private FilterConfig filterConfig;

    @Mock
    private ServletRequest request;



    @Before
    public void setUp(){
        accessFilter = new AccessFilter();

        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testAccessFilter() throws Exception {
        accessFilter.init(filterConfig);


        accessFilter.destroy();
    }
}

Without the Mock request, this test passes. But when I add the request mock, just as attribute, I get following exception:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletInputStream

at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2615) at java.lang.Class.getDeclaredMethods(Class.java:1860) at org.mockito.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:349) at org.mockito.cglib.proxy.Enhancer.getMethods(Enhancer.java:427) at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:457) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:110) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56) at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51) at org.mockito.Mockito.mock(Mockito.java:1243) at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:30) at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16) at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43) at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66) at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71) at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55) at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108) at com.trax.watson.AccessFilterTest.setUp(AccessFilterTest.java:31)

Line 31 is: MockitoAnnotations.initMocks(this);

What is going wrong?

Aucun commentaire:

Enregistrer un commentaire