简单易用
<!--test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
建一个AbstractTest类文件
import junit.framework.Assert;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:config/appcontext-*.xml"})
@ActiveProfiles("test")
public class AbstractTest {
protected void print(Object obj) {
System.out.println(ToStringBuilder.reflectionToString(obj, ToStringStyle.MULTI_LINE_STYLE));
}
private long start;
public void notNull(Object obj) {
assertNotNull(obj);
}
public void isNull(Object obj) {
assertNull(obj);
}
public void equal(Object expected, Object actual) {
Assert.assertEquals(expected, actual);
}
@Before
public void start() {
this.start = System.currentTimeMillis();
}
@After
public void end() {
System.out.println("耗时: " + (System.currentTimeMillis() - start) + "ms");
}
@Test
public void test() throws Exception {
}
}
ContextConfiguration这个指向本地的xml文件
建一个测试类Test1 继承AbstractTest
public class Test1 extends AbstractTest{
@Test
public void testGetById() throws Exception {
Integer accountId = 99;
System.out.println("6666666666666");
}
}