2

I'm having difficulties understanding how to mock/stub an interface with Mockito.

This simple scenario...

public interface Adder
{
    int add(int a, int b);
}

public class Calculator
{
    Adder _adder;

    public Calculator(Adder adder)
    {
        _adder = adder;
    }

    public int add(int a, int b)
    {
        return _adder.add(a, b);
    }
}

public class MockitoTester
{
    @Test
    public void can_mock()
    {
        Adder adder = mock(Adder.class);
        Calculator sut = new Calculator(adder);
        assertThat(sut).isNotNull();
    }
}

Is giving me this error...

java.lang.NoClassDefFoundError: net/bytebuddy/dynamic/scaffold/subclass/ConstructorStrategy
    at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.<init>(CachingMockBytecodeGenerator.java:20)
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.<init>(ByteBuddyMockMaker.java:22)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at java.lang.Class.newInstance(Class.java:442)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:33)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:12)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:11)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:23)
    at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:44)
    at org.mockito.Mockito.<clinit>(Mockito.java:1101)
    at com.jnericks.testutils.MockitoTester.can_mock(MockitoTester.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 40 more
M A
  • 71,713
  • 13
  • 134
  • 174
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174
  • 4
    You're missing a library (whatever bytebuddy is) from your runtime classpath. – Sotirios Delimanolis Jul 12 '15 at 15:42
  • 2
    http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception?lq=1 – Sotirios Delimanolis Jul 12 '15 at 15:43
  • ok, thank you. i thought any libraries/dependencies would automatically be pulled in by maven. i guess not. I come from the C#/.net/nuget world and am trying to work in java/maven and it is proving to not be a seamless transition lol – Jon Erickson Jul 12 '15 at 15:45
  • Btw what do you want to achieve with this check `assertThat(sut).isNotNull();`? If the program reaches it, it will always be `true`. – Tom Jul 12 '15 at 15:53
  • I'm not trying to achieve anything other than making sure the tools are loaded and working. – Jon Erickson Jul 12 '15 at 15:57

1 Answers1

6

Mockito depends on the artifact net.bytebuddy:byte-buddy. See this POM definition from the Maven repositories. Make sure you get the mockito-all artifact which includes all dependencies in one Jar.

If you are using Maven, then it should automatically resolve the dependency when using mockito-core. You can run mvn dependency:tree on the project to see the hierarchy of your project's dependencies.

M A
  • 71,713
  • 13
  • 134
  • 174