1

当我尝试在 junit 测试类中加载我的配置文件时,我在注入数据源时遇到以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException:在 org.springframework.beans.factory.support 的 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) 中没有定义名为“transactionManager”的 bean。 AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1094) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java: 196) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079) 在 org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:308) 在 org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:358) 在 org.springframework.test.context 的 org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:158) .junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements .SpringRepeat.evaluate(SpringRepeat.java:72) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在org.junit.runners。ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit .runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks .java:61) 在 org.springframework 的 org.junit.runners.ParentRunner.run(ParentRunner.java:236) 的 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)。 test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org .eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt .internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)390)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)390)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

请任何人都可以帮助我解决这个问题。

提前致谢

4

1 回答 1

1

看来您无法访问“transactionManager”。如我所见,您进行了单元测试,然后我想您不访问 ApplicationContext.xml 文件(或声明您的 bean 的任何位置)。

对于单元测试,您应该通过注释显式地给出 bean 文件。

这些注释对我有用:

@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
@ContextConfiguration(locations={"classpath:applicationContext.xml",
    "classpath:applicationContext-dao.xml",
    "classpath:applicationContext-services.xml",
    "classpath:/com/xxx/zzz/test/CompanyServiceTest-context.xml"})
public class CompanyServiceTest extends AbstractTransactionalJUnit4SpringContextTests {
private static Log LOG = LogFactory.getLog(CompanyServiceTest.class);

@Autowired
public CompanyService companyService;

@Before
public void setUp() {
    System.err.println("Clear the mailboxes...");
    Mailbox.clearAll();
}

    @Test
    public void testCreateCompany() throws IOException, BusinessException, MessagingException {
        LOG.info("Creating the company DEMO...");
    }

}

于 2012-12-03T11:29:43.503 回答