我不知道为什么我的模仿错误

java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd
at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(ClassImposterizer.java:36)
at org.mockito.internal.creation.jmock.ClassImposterizer.<clinit>(ClassImposterizer.java:29)
at org.mockito.internal.util.MockCreationValidator.isTypeMockable(MockCreationValidator.java:17)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:133)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:127)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:50)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)
at fr.oap.SubscriptionTest.testGetSubscriptionById(SubscriptionFactoryTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.objenesis.ObjenesisStd
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
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)
... 29 more


关于我的测试班是这样的:

import junit.framework.TestCase;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import fr.aop.subscription.AbstractSubscription;
public class SubscriptionTest extends TestCase {
@Test
public void testGetSubscriptionById() {
    ArgumentCaptor<AbstractSubscription>
arg=ArgumentCaptor.forClass(AbstractSubscription.class);
     Subscription objMock=Mockito.mock(Subscription.class);
    Mockito.when(objMock.getSubscribById(1)).thenReturn(arg.getValue());
}
}


关于方法getSubscribById whitch的信息位于Subscription类中:

@Override
public AbstractSubscription getSubscriptionById(final Integer id) {
    this.log.debug("BEGIN: getSubscriptionById id = " + id);
    AbstractSubscription obj = null;
    if (id != null) {
        final StringBuilder queryString = new StringBuilder("select c from AbstractSubscription c ");

        try {
            queryString.append("where c.id = :id");
            Query query = this.getEntityManager().createQuery(queryString.toString());
            query = query.setParameter("id", id);
            obj = (AbstractSubscription) query.getSingleResult();
        } catch (final Exception exc) {

        }
    }
    return obj;
}


当我实例化Subcription类时,它要求我与数据库建立连接,这就是为什么我想逃避此问题并寻找诸如mockito的解决方案

最佳答案

ClassNotFoundException是无法加载特定类的类加载器的结果。

在您的情况下,Mockito具有对Objenesis的传递依赖项(它需要Objenesis才能确保正确的行为)。您最有可能尝试在测试类路径上使用Mockito执行测试,但没有Objenesis。

您需要将Objenesis添加到测试类路径。

对于Maven项目,请确保:


您已将Mockito声明为测试依赖项

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>

从命令行执行特定测试

mvn test -Dtest=fullyQualifedNameToYourTestClass

09-09 16:54