一。应用场景:普通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)

  } 

 }

Spring与junit4集成测试的更多相关文章

  1. 一文教会你如何在 Spring 中进行集成测试,太赞了

    不得不说,测试真的是太重要了!但并不是所有的开发者都这样认为,这种感觉在我回到洛阳后尤其强烈.竟然有团队成员不经测试就把代码提交到代码库,并且是会报错的那种,我天呐,遇到这种队友我也是醉了. 我之前是 ...

  2. 利用Spring的junit4测试

    利用Spring的JUnit4进行测试 不需要再显式创建Spring容器和getBean @RunWith(SpringJUnit4ClassRunner.class) @ContextConfigu ...

  3. Spring整合Junit4

    1.加入相应依赖包 junit4-4.7.jar 以及spring相关jar包 2.在测试代码的源码包中如 src/test/java 新建一个抽象类如下 import org.junit.runne ...

  4. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  5. Spring 整合 Junit4 进行单元测试

    1. pom.xml 引入JAR依赖: <dependency> <groupId>junit</groupId> <artifactId>junit& ...

  6. Spring整合JUnit4进行AOP单元测试的时候,报:"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=64

    错误代码 "C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size= ...

  7. Spring Test+JUnit4整合使用测试ZZJ_淘淘商城项目:day01(RESTful Web Service)

    针对整合的Dao层与Service层,在做spring与通用Mapper和分页插件相关测试时比较麻烦.如果只用JUnit测试,需要每次Test方法里初始化一下applicationContext,效率 ...

  8. Spring整合JUnit4测试

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/ap ...

  9. Spring MVC Junit4 单元測试 JunitTest

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmVyb21lX3M=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

随机推荐

  1. js中bind解析

    一.arguments的含义 // arguments 是一个对应于传递给函数的参数的类数组对象 function a(){ console.log(arguments); } a(); // Arg ...

  2. 关于微信XML解析存在的安全问题

    ---恢复内容开始--- 前言: 最近微信官方提出:微信支付商户,最近暴露的XML外部实体注入漏洞(XML External Entity Injection,简称 XXE),该安全问题是由XML组件 ...

  3. 【JVM学习笔记】字节码文件结构

    https://www.cnblogs.com/heben/p/11468285.html  比这篇笔记更好一点 新建一个Java类 package com.learn.jvm; public cla ...

  4. SpringBoot: 1.创建第一个SpringBoot项目(转)

      一.新建项目 二.打开项目的pom文件,在里面添加maven依赖 1 <!--springboot项目依赖的父项目--> 2 <parent> 3 <groupId& ...

  5. 【笔记】Docker入门

    这个文章讲的比较透彻,就不复制粘贴了 <Docker从入门到实践>阅读笔记 Docker安装 环境 root@fudonghai:~# uname -a Linux fudonghai - ...

  6. C语言递归之对称二叉树

    题目描述 给定一个二叉树,检查它是否是镜像对称的. 示例 二叉树 [1,2,2,3,4,4,3] 是对称的. / \ / \ / \ [1,2,2,null,3,null,3] 则不是镜像对称的. / ...

  7. 论文阅读 | Tackling Adversarial Examples in QA via Answer Sentence Selection

    核心思想 基于阅读理解中QA系统的样本中可能混有对抗样本的情况,在寻找答案时,首先筛选出可能包含答案的句子,再做进一步推断. 方法 Part 1 given: 段落C   query Q 段落切分成句 ...

  8. 第六周&java实验报告四

    实验报告: 一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:rad ...

  9. interface和struct

    interface: C++关键字中没有interface,即接口.interface和class不同,interface仅有接口声明,而且所有声明默认的访问权限是public而非private. C ...

  10. spark教程(18)-sparkSQL 自定义函数

    sparkSQL 也允许用户自定义函数,包括 UDF.UDAF,但没有 UDTF 官方 API class pyspark.sql.UDFRegistration(sparkSession)[sour ...