0

嗨,我正在尝试将我的 DaoTest 类测试为 junit 测试。在运行该类时,我得到:

java.lang.NoClassDefFoundError: org/springframework/beans/factory/NoUniqueBeanDefinitionException。

我查看了互联网,发现这可能不仅仅是因为在配置文件中引用了您的 bean。但我无法弄清楚我如何对豆类有不止一个参考。

这里是UserDaoTest类:

@ContextConfiguration (locations = "classpath:persistence-beans.xml")
public class UserDaoTest extends DomainAwareBase {

@Autowired
@Qualifier("userDao")
private UserDao userDao;

@Test
public void testUpdate() {

    int size = userDao.list().size();

    User user = new User(1, "jimmy", "password", "admin", "sales");

    userDao.add(user);

    Assert.assertTrue(size < userDao.list().size());

}

@Test
public void testFind() {
    User user = new User(1, "jimmy", "password", "admin", "sales");

    userDao.add(user);
    User found = userDao.find(user.getUserId());

    Assert.assertEquals(user, found);
}

@Test
public void testList() {
    Assert.assertEquals(0, userDao.list().size());
    User user1 = new User(1, "jimmy", "password", "admin", "Sales");
    User user2 = new User(2, "pankit", "pankit", "admin", "HR");
    User user3 = new User(3, "abc", "abc", "admin", "Engineer");

    List<User> users = Arrays.asList(user1, user2, user3);

    for (User user : users) {

        userDao.add(user);
    }

    List<User> found = userDao.list();
    Assert.assertEquals(3, userDao.list().size());

    for (User user : found) {

        Assert.assertTrue(users.contains(user));

    }

}

@Test
public void testRemove() {

    User user1 = new User(1, "jimmy", "password", "admin", "Sales");

    userDao.add(user1);
    Assert.assertEquals(user1, userDao.find(user1.getUserId()));

    userDao.remove(user1);
    Assert.assertNull(userDao.find(user1.getUserId()));


}

}

这是我的persistence-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<!-- we can use annotations -->
<context:annotation-config />

<!-- package to look for annotated classes -->
<context:component-scan base-package="com.finalproject" />

<!-- we will manage transactions with annotations -->
<tx:annotation-driven />

<!-- data source for our database -->

<bean id="dataSource"

    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName"
        value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />

    <property name="url"
        value="jdbc:mysql://localhost:3306/leadmanagement_db" />
    <property name="username" value="root" />

    <property name="password" value="root" />

</bean>

<bean id="transactionManager"

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- configure hibernate session factory -->

<bean id="sessionFactory"

    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />

    <property name="configLocation">


        <value>classpath:hibernate.cfg.xml</value>


    </property>

    <property name="hibernateProperties">

        <props>

            <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>

            <prop key="hibernate.show_sql">true</prop>

            <prop key="hibernate.hbm2ddl.auto">update</prop>

        </props>

    </property>

</bean>

编辑 这是我的堆栈跟踪

java.lang.NoClassDefFoundError: org/springframework/beans/factory/NoUniqueBeanDefinitionException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getDeclaredMethods(Class.java:1808)
at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:159)
at org.springframework.context.annotation.ConfigurationClassUtils.isLiteConfigurationCandidate(ConfigurationClassUtils.java:106)
at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:87)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:233)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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)

引起:java.lang.ClassNotFoundException: org.springframework.beans.factory.NoUniqueBeanDefinitionException at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355)在 java.security.AccessController.doPrivileged(Native Method) 在 java.net.URLClassLoader.findClass(URLClassLoader.java:354) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:423) 在 sun.misc.Launcher$AppClassLoader .loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 41 更多

4

1 回答 1

0

您的类NoUniqueBeanDefinitionException路径中不存在类。将spring-beans依赖项/jar包含到您的测试类路径中,然后重试。

于 2013-04-08T11:11:26.980 回答