Easymock - 模拟内部类/接口

时间:2017-03-10 11:21:16

标签: java java-ee junit mocking easymock

我在内部界面创建模拟时遇到问题。

我想从javax.ws.rs.client

模拟HttpClient

所以我按照以下方式做了这件事。

mockedClient = createMock( Client.class );
webTarget = createMock(WebTarget.class);
Invocation.Builder  builder = createMock( Invocation.Builder.class ); //<-error
response = createMock( Response.class );

您是否知道如何使用EasyMock进行操作? 我想知道实现这个接口并模拟我在实现中使用的方法。这是一个很好的解决方法吗?

此界面如何显示:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package javax.ws.rs.client;

import java.util.Locale;
import java.util.concurrent.Future;
import javax.ws.rs.client.AsyncInvoker;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.SyncInvoker;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

public interface Invocation {
    Invocation property(String var1, Object var2);

    Response invoke();

    <T> T invoke(Class<T> var1);

    <T> T invoke(GenericType<T> var1);

    Future<Response> submit();

    <T> Future<T> submit(Class<T> var1);

    <T> Future<T> submit(GenericType<T> var1);

    <T> Future<T> submit(InvocationCallback<T> var1);

    public interface Builder extends SyncInvoker {
        Invocation build(String var1);

        Invocation build(String var1, Entity<?> var2);

        Invocation buildGet();

        Invocation buildDelete();

        Invocation buildPost(Entity<?> var1);

        Invocation buildPut(Entity<?> var1);

        AsyncInvoker async();

        Invocation.Builder accept(String... var1);

        Invocation.Builder accept(MediaType... var1);

        Invocation.Builder acceptLanguage(Locale... var1);

        Invocation.Builder acceptLanguage(String... var1);

        Invocation.Builder acceptEncoding(String... var1);

        Invocation.Builder cookie(Cookie var1);

        Invocation.Builder cookie(String var1, String var2);

        Invocation.Builder cacheControl(CacheControl var1);

        Invocation.Builder header(String var1, Object var2);

        Invocation.Builder headers(MultivaluedMap<String, Object> var1);

        Invocation.Builder property(String var1, Object var2);
    }
}

来自控制台的例外:

java.lang.ExceptionInInitializerError
at java.lang.J9VMInternals.initialize(J9VMInternals.java:258)
at java.lang.Class.forNameImpl(Native Method)
at java.lang.Class.forName(Class.java:182)
at com.sun.proxy.$Proxy16.<clinit>(Unknown Source)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:236)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:80)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
at java.lang.reflect.Constructor.newInstance(Constructor.java:539)
at java.lang.reflect.Proxy.newInstance(Proxy.java:753)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:744)
at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:30)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:113)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:94)
at org.easymock.EasyMock.niceMock(EasyMock.java:181)
at org.easymock.EasyMock.createNiceMock(EasyMock.java:349)
at 
at 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:88)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:613)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:152)
at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
at javax.ws.rs.core.Cookie.<clinit>(Cookie.java:61)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:236)
... 41 more Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
at java.lang.Class.forName(Class.java:182)
at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:114)
at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:207)
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:135)
... 45 more

编辑:

当我将mockito添加到项目并模拟此对象时:

Invocation.Builder  builder = mock( Invocation.Builder.class );

有效!

所以问题是,为什么这个模拟在EasyMock中不起作用?

1 个答案:

答案 0 :(得分:0)

Invocation.Builder正在使用名为CacheControl的类。该类有一个静态字段,调用RuntimeDelegate.getInstance()。因此需要RuntimeDelegate。默认RuntimeDelegateorg.glassfish.jersey.internal.RuntimeDelegateImpl,不在您的类路径中。这就是你得到ClassNotFoundException的原因。

Mockito的工作原因很奇怪。他们使用ByteBuddy实现他们的模拟。由于某种原因,这不需要加载CacheControl

EasyMock使用Proxy.newProxyInstance模拟界面。这看起来确实加载了CacheControl。所以它失败了。

但是,如果您随后使用Mockito致电builder.cacheControl(new CacheControl());,则会收到相同的错误。