Spring test 整合測試 續

來源:互聯網
上載者:User

  昨天瞭解了一下使用Spring的test架構做Junit的整合單元測試,一般情況下,我們的測試類別是繼承自抽象類別AbstractDependencyInjectionSpringContextTests,需要注意的就是要重寫方法getConfigLocation()。這樣的測試類別有個優點就是每個測試方法Spring容器都只對beans初始化一次。        但是,有些情況下我們不想在單元測試的時候破壞資料庫現場,這時候可以是測試類別繼承抽象類別AbstractTransactionalSpringContextTests。使用這個抽象類別的時候,一定要注意屬性TransactionManager屬性的注入,以及TransactionManager的DataSource屬性的注入,否則測試類別是不能夠初始化的,會拋出異常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ali.luna.crm.test.biz.train.TrainingBOImplTest': Unsatisfied dependency expressed through bean property 'transactionManager': Set this property value or disable dependency checking for this bean.at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:1184)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1006)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:205)at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:180)at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:100)at junit.framework.TestCase.runBare(TestCase.java:125)at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)at junit.framework.TestResult$1.protect(TestResult.java:106)at junit.framework.TestResult.runProtected(TestResult.java:124)at junit.framework.TestResult.run(TestResult.java:109)at junit.framework.TestCase.run(TestCase.java:118)at junit.framework.TestSuite.runTest(TestSuite.java:208)at junit.framework.TestSuite.run(TestSuite.java:203)at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)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)        俺被這個坑困擾了一天。        若是想瞭解更多Spring單元測試相關,可訪問如下連結:        http://www.blogjava.net/vcycyv/archive/2011/02/24/345040.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.