Spring与junit4集成测试

一。应用场景:普通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 org.junit.internal.requests.ClassRequest@31befd9f
    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)

  } 

 }

转载于:https://www.cnblogs.com/lingtiaoti/p/9534520.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值