spring集成junit测试

导入maven坐标

可以自动加载上下文配置,不用手动加载容器
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-test</artifactId>
	    <version>5.3.10</version>
	    <scope>test</scope>
	</dependency>
	<!-- https://mvnrepository.com/artifact/junit/junit -->
	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.13.2</version>
	    <scope>test</scope>
	</dependency>

code test

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
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.allinpay.spring.testbean.service.TestBeanService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-testbean.xml")
public class TestBean {
	
	@Autowired
	private TestBeanService testBeanService;
	
	@BeforeClass
	public static void beforeClass(){
		System.out.println("beforeclass...");
	}
	
	@AfterClass
	public static void afterClass(){
		System.out.println("afterclass...");
	}
	
	@Before
	public void before(){
		System.out.println("before...");
	}
	
	@After
	public void after(){
		System.out.println("after...");
	}
	
	@Test(expected=Exception.class,timeout=1)
	public void test1(){
		//ApplicationContext app = new ClassPathXmlApplicationContext("spring-testbean.xml");
		//TestBeanService testBeanService = (TestBeanServiceImpl) app.getBean("testBeanService");
		System.out.println("test1...");
		System.out.println(1/0);
		testBeanService.save();
	}
	
	@Test
	//@Ignore
	public void test2(){
		System.out.println("test2...");
	}
	
}

注解详解

@RunWith(SpringJUnit4ClassRunner.class):表示让测试运行于Spring环境
@ContextConfigration:加载spring的容器
@Test: 测试的方法,两个参数,
	expected:测试的方法应该抛出的异常,抛出其它异常测试失败。
	timeout:测试方法的超时时间,超出时间测试失败
@Ignore:忽略的测试的方法或者类
@Before:每个测试方法前执行
@After:每个测试方法后执行
@BeforeClass:整个测试前执行
@AfterClass:整个测试后执行

测试结果

十一月 01, 2021 10:47:14 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
十一月 01, 2021 10:47:14 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@68c4039c, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@ae45eb6, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@59f99ea, org.springframework.test.context.support.DirtiesContextTestExecutionListener@27efef64, org.springframework.test.context.event.EventPublishingTestExecutionListener@6f7fd0e6]
beforeclass...
before...
test1...
save success...
after...
before...
test2...
after...
afterclass...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值