导入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(){
System.out.println("test1...");
System.out.println(1/0);
testBeanService.save();
}
@Test
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...