简体   繁体   中英

Mock base class method with Easymock

I have the following test code:

    herder = PowerMock.createPartialMock(DistributedHerder.class, new String[]{"backoff", "updateDeletedConnectorStatus"},
            new DistributedConfig(HERDER_CONFIG), worker, WORKER_ID, statusBackingStore, configBackingStore, member, MEMBER_URL, time);

    configUpdateListener = herder.new ConfigUpdateListener();
    rebalanceListener = herder.new RebalanceListener();
    plugins = PowerMock.createMock(Plugins.class);
    EasyMock.expect(worker.getPlugins()).andReturn(plugins);
    EasyMock.expect(herder.connectorType(CONN1)).andReturn(ConnectorType.SOURCE).anyTimes();

where connectorType() method is defined in AbstractHerder, base class of DistributedHerder. Since the herder is created off of DistributedHerder, easy mock doesn't find mock for AbstractHerder class, leading to NPE:

org.apache.kafka.connect.runtime.distributed.DistributedHerderTest > testInconsistentConfigs FAILED
  java.lang.NullPointerException
    at org.apache.kafka.connect.runtime.AbstractHerder.getConnector(AbstractHerder.java:342)
    at org.apache.kafka.connect.runtime.distributed.DistributedHerder$$EnhancerByCGLIB$$af0dd240.CGLIB$getConnector$39(<generated>)
    at org.apache.kafka.connect.runtime.distributed.DistributedHerder$$EnhancerByCGLIB$$af0dd240$$FastClassByCGLIB$$8c31132d.invoke(<generated>)
    at org.easymock.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:92)
    at org.apache.kafka.connect.runtime.distributed.DistributedHerder$$EnhancerByCGLIB$$af0dd240.getConnector(<generated>)
    at org.apache.kafka.connect.runtime.AbstractHerder.connectorType(AbstractHerder.java:349)
    at org.apache.kafka.connect.runtime.distributed.DistributedHerder$$EnhancerByCGLIB$$af0dd240.CGLIB$connectorType$23(<generated>)
    at org.apache.kafka.connect.runtime.distributed.DistributedHerder$$EnhancerByCGLIB$$af0dd240$$FastClassByCGLIB$$8c31132d.invoke(<generated>)
    at org.easymock.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:92)
    at org.apache.kafka.connect.runtime.distributed.DistributedHerder$$EnhancerByCGLIB$$af0dd240.connectorType(<generated>)
    at org.apache.kafka.connect.runtime.distributed.DistributedHerderTest.setUp(DistributedHerderTest.java:179)

Any hint is welcome.

I am a bit lost here but will still try to give some hints in terms of Socratic questions.

  • Why do you need PowerMock for DistributedHerder ? It looks like a normal partial mocking
  • And why PowerMock for Plugins instead of plain EasyMock?
  • Do you have the PowerMock runner configured?
  • What is herder.new ConfigUpdateListener() ? Which language is that?
  • What is worker ?
  • I see no replay() indeed. Can we have the full code?
  • Nothing is recorded for backoff and updateDeletedConnectorStatus . Is that normal?
  • In your case, it seems the plugins() method is returning null. This one isn't mocked so it probably just return null indeed no?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM