Spring测试

导入spring测试的jar包

在这里插入图片描述
Spring测试必须保证Eclipse的单元测试的最低版本是 4.12版本,如果使用的Eclipse版本很低,那么单元测试版本可能低于4.12,那么需要开发者手动导入单元测试的jar包。
Spring测试必须使用单元测试测试,不可以使用main方法

如果使用Spring方式测试,必须使用两个注解

@RunWith注解
表示先启动Spring容器,把junit运行在Spring容器中
@ContextConfiguration注解
从哪里加载资源文件,默认从src(源目录)下面加载

xml方式配置

package com.spring.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;

import com.spring.config.SpringConfig;
import com.spring.controller.CustomController;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class SpringTest {
	
	@Autowired
	private CustomController controller;

	@Test
	public void testInsert() throws Exception {
		controller.insert();
	}
	
	@Test
	public void testDelete() throws Exception {
		controller.delete();
	}

}

纯注解方式的配置

package com.spring.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;

import com.spring.config.SpringConfig;
import com.spring.controller.CustomController;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class SpringTest {
	
	@Autowired
	private CustomController controller;

	@Test
	public void testInsert() throws Exception {
		controller.insert();
	}
	
	@Test
	public void testDelete() throws Exception {
		controller.delete();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值