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 [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)

  } 

 }

原文地址:https://www.cnblogs.com/lingtiaoti/p/9534520.html

时间: 2024-08-30 00:49:30

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

Spring MVC Junit4 单元测试

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/config/spring3/applicationContext.xml" })//启动Spring容器 public class TestISrmVendorService { //注入Spring容器中的Bean @Autowired private ISrmVendorService srmVendorService; @T

Spring整合Junit4

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

web项目中 集合Spring&amp;使用junit4测试Spring

web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloService"); helloService.sayHello(

Spring整合Junit4进行单元测试

一. 添加依赖包(maven) <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframewor

Spring整合JUnit4测试时,使用注解引入多个配置文件

一般情况下: [html] view plain copy @ContextConfiguration(Locations="../applicationContext.xml") 多个文件时,可用{} [html] view plain copy @ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })

06_在web项目中集成Spring

在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloS

Spring MVC -- 应用测试

测试在软件开发中的重要性不言而喻.测试的主要目的是尽早发现错误,最好是在代码开发的同时.逻辑上认为,错误发现的越早,修复的成本越低.如果在编程中发现错误,可以立即更改代码:如果软件发布后,客户发现错误所需要的修复成本会很大. 在软件开发中有许多不同的测试,其中两个是单元测试和集成测试.通常从单元测试开始测试类中的单个方法,然后进行集成测试,以测试不同的模块是否可以无缝协同工作. 本篇博客中的示例使用JUnit测试框架以及Spring test模块.Spring test模块中的API可用于单元测

Spring体系

1.Spring简介 Spring是一个轻量级Java开发框架,最早有Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题.它是一个分层的JavaSE/JavaEE full-stack(一站式)轻量级开源框架,为开发Java应用程序提供全面的基础架构支持.Spring负责基础架构,因此Java开发者可以专注于应用程序的开发. Spring的优点: (1)方便解耦,简化开发 Spring就是一个大工厂,可以将所有对象创建和依赖的关系维护,交给Spring管理.

Spring 框架基础(01):核心组件总结,基础环境搭建

本文源码:GitHub·点这里 || GitEE·点这里 一.Spring框架 1.框架简介 Spring是一个开源框架,框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益.简单来说,Spring是一个分层的轻量级开源框架. 2.