Java junit 测试

本文详细介绍如何在Spring框架中整合各类组件,并通过Spring Test进行单元测试。文章包含配置文件applicationContextMixed.xml的解析,日志配置log4j.properties的设置,以及HelloService类的示例代码。此外,还提供了SpringTest类的示例,展示如何使用SpringJUnit4ClassRunner和ContextConfiguration进行测试。

需要架包:

spring-test-4.2.4.RELEASE.jar

spring framework 使用的架包:

spring-beans-4.2.4.RELEASE.jar

Spring 使用的日志架包:

com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsoruce.org.apache.log4j-1.2.16.jar

applicationContextMixed.xml 文件

<?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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->

	<bean id="produceService" class="com.igeek.ioc.mixed.ProduceService"></bean>
	<bean id="produceDao" class="com.igeek.ioc.mixed.ProduceDao"></bean>
	<bean id="helloService" class="com.igeek.test.HelloService"></bean>
<!--扫描包下的类, 查看那个类上有@Compontent注解,把这个类的对象new 出来 -->
	<!-- <context:component-scan base-package="com.igeek.ioc"></context:component-scan> -->
</beans>

log4j.properties

### dircet log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p %c{1}:%L - %m%n

### dircet message to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels  - for more verbose logging change 'info' to 'debeg' ###


log4j.rootLogger = info,stdout

测试代码文件:
HelloService.java

package com.igeek.test;

/**
 * @author Administrator
 *
 */

public class HelloService {

	/**
	 * 
	 */
	public void sayHello() {
		// TODO Auto-generated method stub
		System.out.println("Hello world !");
	}
	
}

测试文件 SpringTest.java

package com.igeek.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContextMixed.xml")
public class SpringTest {

	@Autowired
	private HelloService helloService;
	
	/**
	 * 
	 */
	@Test
	public void test() {
		// TODO Auto-generated method stub
		helloService.sayHello();
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值