常用annotation
package model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.junit.*;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
/**
* Created by dongyu.liu on 2017-5-22.
*/
public class TestTeacher {
@Before
public void before(){
// 在每个测试方法之前执行
System.out.println("before");
}
@After
public void after(){
// 在每个测试方法之后执行
System.out.println("after");
}
@BeforeClass
public static void beforeClass(){
// 测试类初始化之前执行
// 一般可以写一些 搭建耗费时间环境(数据库连接、加载配置文件等等)
System.out.println("beforeClass");
}
@AfterClass
public static void afterClass(){
// 测试类初始化之后执行
// 一般可关闭资源等
System.out.println("afterClass");
}
@Test
// 该方法在1000毫秒内执行完毕 @Test(timeout = 1000)
// @Ignore 这个方法暂时忽略 先不测试
public void TestTeacher(){
int a =4;
assertEquals(a, 4);
assertThat(a,is(4));
}
public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("中级");
// Configuration cfg = new Configuration();
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session sessions = sf.openSession();
sessions.beginTransaction();
sessions.save(t);
sessions.getTransaction().commit();
sf.close();
}
}
常用pom.xml配置
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
</dependency>
idea中插件使用
- File–>settings–>Plguins–>Browse repositories–>输入JUnit–>选择JUnit Generator V2.0安装。
- 在你要创建测试用例的类中按 Ctrl + Shift + T 即可
xml文件无法正常编译到target/class中
在pom.xml中添加即可
<project>
<build>
<finalName>mybatistest</finalName>
<!-- xml文件正常编译到class目录中-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>