当我尝试在junit测试类中加载配置文件时,我在注入数据源时遇到错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'transactionManager'的bean at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) 在org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1094) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:308) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:158) 在org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:358) 在org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73) 在org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) 在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) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184) 在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 在org.junit.runners.ParentRunner.run(ParentRunner.java:236) 在org.springframework.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)
请允许任何人帮助我。
提前致谢
答案 0 :(得分: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...");
}
}