zoukankan      html  css  js  c++  java
  • Spring入门(11)-Spring与Junit整合

    POM配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.chzhao</groupId>
    	<artifactId>springjunittest</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>springjunittest</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>4.0.1.RELEASE</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>4.0.1.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>4.0.1.RELEASE</version>
    		</dependency>
    	</dependencies>
    </project>
    
    

    代码

    package com.chzhao.springjunittest;
    
    public class Dao {
    	public void saySomething(String something) {
    		System.out.println("say: " + something);
    	}
    }
    

    配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.0.xsd"	>
    	<bean  name="dao" class="com.chzhao.springjunittest.Dao"/>  
    </beans>
    

    测试类

    package com.chzhao.springjunittest;
    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @SuppressWarnings("restriction")
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:applicationContext.xml")
    public class DaoTest {
    	@Resource
    	private Dao dao ;
    	@Test
    	public void sayTest(){
    		this.dao.saySomething("liang");
    	}
    }
    
    

    需要注意的地方

    • 测试类加载applicationContext.xml的时候不需要改变原来applicationContext.xml的位置,locations = "classpath:applicationContext.xml"就好。
  • 相关阅读:
    MVC3.0与C#截取字符串
    MVC3.0图片滚动和相册展示(上)
    MVC3.0视频点播及上传格式转化
    职位VS能力
    liblfds 测试
    dpdk 相关概念
    WAR文件
    在word中选择一个矩形区域
    IP地址 网段的划分
    ipconfig...ping...netstat
  • 原文地址:https://www.cnblogs.com/wardensky/p/4251513.html
Copyright © 2011-2022 走看看