views:

221

answers:

3

Hello good people! 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

<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.

+1  A: 

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):

Before

Methods annotated with this marker are executed by the JUnit framework before it calls the next test case method.

After

Methods annotated with this marker are executed by JUnit after the actual test case method has been run.

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.

AfterClass

Methods tagged with this annotation will be executed only once (after JUnit has run the last test case).

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
Dirk
thank you very good explanation.Thanks man
black sensei
+3  A: 

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

David Rabinowitz
A: 

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.

Brian Deterling
hello! thanks for the reply, and based on that i think i've realised what is the mistake.there is no data for the rest of the method to test.i'm not sure BeforeClass is inserting anything.i've pushed the inserting of initial data inside a testmethod and debug it (since i can't debug from the beforeclass) and while the hbm2ddl is set to create, i can see that the data is inserted b removing the previous one.so i'm a bit confuse about how to get these thing work.i can't post my code inside comments too any suggestion?
black sensei
now i get something! i've set hbm2ddl to create and i did a fake assertion to let my save method successful without actually saving anything, i could see in the database what BeforeClass inserted but once i really save a thing in from the save test method it clears what BeforeClass inserted.How to prenvent that? i'll like to append to it.
black sensei