Spring與junit4整合測試

來源:互聯網
上載者:User

標籤:整合測試   classpath   pat   requests   adt   contex   初始   項目結構   span   

一。應用情境:普通java web整合spring test,

  項目結構:使用的是加入jar的方式,不是maven加入依賴的方式

  注意:如果是引入jar包的方式,例如:spring-test4.0.5.jar 和junit4.12.jar的方式,

  如果是使用的是junit4.12.jar的版本,要加入 hamcrest-core1.3.jar包,否則報如下錯誤:

java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test], {ExactMatcher:fDisplayName=test(org.devin.hellochat.test.Test)], {LeadingIdentifierMatcher:fClassName=org.devin.hellochat.test.Test,fLeadingIdentifier=test]] from [email protected]    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

二。使用:

1.applicationContext.xml spring-mvc.xml設定檔放在resource源檔案下,注意:web.xml檔案需要配置<Context-param></Context-param>,並引入applicationContext.xml檔案,

sevlet配置中需要初始化spring-mvc.xml檔案

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration({"classpath:applicationContext.xml","classpath:spring_mvc.xml"})  

public class Test{

  @Automired

  private ItemCatService itemCatService;

  @Test

  public void test(){

    System.out.println(itemCatService)

  } 

 }

 

2.applicationContext.xml spring-mvc.xml設定檔放在WEB-INF目錄下:

  

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration({"file:WebContent/WEB-INF/applicationContext.xml","file:WebContent/WEB-INF/spring_mvc.xml"})  

public class Test{

  @Automired

  private ItemCatService itemCatService;

  @Test

  public void test(){

    System.out.println(itemCatService)

  } 

 }

Spring與junit4整合測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.