first of all thank you all for reading this. i want to populate my database (derby) every time i run the test class to be able to perform test like delete or update or deletebyid stuffs. I use
首先,感谢大家阅读本文。我想在每次运行测试类时填充我的数据库(derby),以便能够执行删除或更新或deletebyid内容等测试。我用
<property name="hibernate.hbm2ddl.auto">create</property>
in my hibernate.cfg.xml file so i'm expecting the database to be first drop and created each time i run the test. i used the class constructor or setup methods but soon realized that they are called the number of time there is a test method in the class (i assume the beforetest and the rest behave the same). So my question is how do i setup the initial data to work with? thanks for reading.
在我的hibernate.cfg.xml文件中,所以我希望每次运行测试时首先删除数据库并创建。我使用了类构造函数或设置方法,但很快就意识到它们被称为类中测试方法的时间(我假设在测试之前,其余的行为相同)。所以我的问题是如何设置初始数据?谢谢阅读。
3 个解决方案
#1
Assuming JUnit 4: There are two groups of annotations, which can be used to trigger the execution of code before and after running the actual test case method(s):
假设JUnit 4:有两组注释,可用于在运行实际测试用例方法之前和之后触发代码的执行:
Before
Methods annotated with this marker are executed by the JUnit framework before it calls the next test case method.
使用此标记注释的方法在调用下一个测试用例方法之前由JUnit框架执行。
After
Methods annotated with this marker are executed by JUnit after the actual test case method has been run.
在运行实际测试用例方法后,JUnit将执行使用此标记注释的方法。
BeforeClass
Methods marked with this annotation will be executed only once (before JUnit runs the first test case). If I read your post correctly, this is the option you actually want.
标记有此注释的方法将仅执行一次(在JUnit运行第一个测试用例之前)。如果我正确阅读了您的帖子,这是您真正想要的选项。
AfterClass
Methods tagged with this annotation will be executed only once (after JUnit has run the last test case).
使用此批注标记的方法将仅执行一次(在JUnit运行最后一个测试用例之后)。
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class SomeTest {
@Test
public void test1() {
System.out.println("test1");
}
@Test
public void test2() {
System.out.println("test2");
}
@Before
public void setUp() {
// Here goes the code, which makes sure, all tests
// see the same context
System.out.println("setUp");
}
@BeforeClass
public static void setUpGlobals() {
// Expensive hibernate set-up will go here. It is
// called only once
System.out.println("setUpGlobals");
}
}
will produce the output
将产生输出
- setUpGlobals
- setUp
- test1
- setUp
- test2
#2
Take a look at DbUnit, it's a JUnit extension aimed to ease db based application testing. One of its features is to have a pre-defined datasets, which populate the database on the beginning of the test. See more here - http://www.dbunit.org/components.html#dataset
看看DbUnit,它是一个JUnit扩展,旨在简化基于数据库的应用程序测试。其特征之一是具有预定义的数据集,该数据集在测试开始时填充数据库。点击此处 - http://www.dbunit.org/components.html#dataset
#3
For the initial data setup (using the annotations described by Dirk), I've used two different methods. If I really want to test the entire process including the ddl script, I have my BeforeClass completely recreate the database by exec'ing an OS process and running the appropriate command to drop and create for that database type. But most of the time I just clear out the tables at the beginning and end of each test (or class) using Hibernate or SQL deletes. That doesn't test the ddl creation part, but usually the Hibernate configuration and other tests will indicate if your database schema is wrong anyway.
对于初始数据设置(使用Dirk描述的注释),我使用了两种不同的方法。如果我真的想测试整个过程,包括ddl脚本,我让我的BeforeClass通过执行操作系统进程并运行适当的命令来删除和创建该数据库类型来完全重新创建数据库。但是大多数时候我只是使用Hibernate或SQL删除清除每个测试(或类)开头和结尾的表。这不会测试ddl创建部分,但通常Hibernate配置和其他测试将指示您的数据库架构是否仍然是错误的。
#1
Assuming JUnit 4: There are two groups of annotations, which can be used to trigger the execution of code before and after running the actual test case method(s):
假设JUnit 4:有两组注释,可用于在运行实际测试用例方法之前和之后触发代码的执行:
Before
Methods annotated with this marker are executed by the JUnit framework before it calls the next test case method.
使用此标记注释的方法在调用下一个测试用例方法之前由JUnit框架执行。
After
Methods annotated with this marker are executed by JUnit after the actual test case method has been run.
在运行实际测试用例方法后,JUnit将执行使用此标记注释的方法。
BeforeClass
Methods marked with this annotation will be executed only once (before JUnit runs the first test case). If I read your post correctly, this is the option you actually want.
标记有此注释的方法将仅执行一次(在JUnit运行第一个测试用例之前)。如果我正确阅读了您的帖子,这是您真正想要的选项。
AfterClass
Methods tagged with this annotation will be executed only once (after JUnit has run the last test case).
使用此批注标记的方法将仅执行一次(在JUnit运行最后一个测试用例之后)。
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class SomeTest {
@Test
public void test1() {
System.out.println("test1");
}
@Test
public void test2() {
System.out.println("test2");
}
@Before
public void setUp() {
// Here goes the code, which makes sure, all tests
// see the same context
System.out.println("setUp");
}
@BeforeClass
public static void setUpGlobals() {
// Expensive hibernate set-up will go here. It is
// called only once
System.out.println("setUpGlobals");
}
}
will produce the output
将产生输出
- setUpGlobals
- setUp
- test1
- setUp
- test2
#2
Take a look at DbUnit, it's a JUnit extension aimed to ease db based application testing. One of its features is to have a pre-defined datasets, which populate the database on the beginning of the test. See more here - http://www.dbunit.org/components.html#dataset
看看DbUnit,它是一个JUnit扩展,旨在简化基于数据库的应用程序测试。其特征之一是具有预定义的数据集,该数据集在测试开始时填充数据库。点击此处 - http://www.dbunit.org/components.html#dataset
#3
For the initial data setup (using the annotations described by Dirk), I've used two different methods. If I really want to test the entire process including the ddl script, I have my BeforeClass completely recreate the database by exec'ing an OS process and running the appropriate command to drop and create for that database type. But most of the time I just clear out the tables at the beginning and end of each test (or class) using Hibernate or SQL deletes. That doesn't test the ddl creation part, but usually the Hibernate configuration and other tests will indicate if your database schema is wrong anyway.
对于初始数据设置(使用Dirk描述的注释),我使用了两种不同的方法。如果我真的想测试整个过程,包括ddl脚本,我让我的BeforeClass通过执行操作系统进程并运行适当的命令来删除和创建该数据库类型来完全重新创建数据库。但是大多数时候我只是使用Hibernate或SQL删除清除每个测试(或类)开头和结尾的表。这不会测试ddl创建部分,但通常Hibernate配置和其他测试将指示您的数据库架构是否仍然是错误的。