2

I am making Test of My classes so I am inserting so many data for to test my code.

So I am thinking to make some mechanism of savepoint and rollback in DB.

I am using postgresql as DB sever.

Following is my code for test :

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/ls-dispatcher-servlet.xml")
public class AddBindingProcessorTest extends IntegrationTestBase {

    @Autowired
    private AddBindingProcessor processor;


    public AddBindingProcessorTest(){

    }

     @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void add() throws Exception {
        AddBinding command;
        command = new AddBinding();
        command.setId(50l);
        command.setBindingName("bindingtest1");
        command.setBindingPrice((double)253);

        BindingTypeResponse response = (BindingTypeResponse)processRequest(command);
        System.out.println("from addbindingprocessor test "+response.getBindingName());
    }
}

Here I am setting value through command object and passing to ProcessRequest() Method that will store data inside DB using hibernate. Still I have to write assert in my testProcess() method that will check data is correct or not ?

So my question is that I when this transaction starts in setUp() method one savepoint should be created and then testProcess() method will be executed and assert check for the data that they are correct or not and then in tearDown() method I want to rollback to savepoint that is set in setUp() method.

So how to do so ? If Anyone can just guide me that what I ll have use and how to move forward then I ll learn that thing and go by myself.

I just want guidance about it that what I ll have to use and where ? Thank You All.

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62

1 Answers1

2

If I get you right, you can just use the

@TransactionConfiguration(defaultRollback = true)

annotation below your @ContextConfiguration annotation. This will rollback the changes in your tests after every run.

user3145373 pointed out, that the attribute transactionManager="context bean transaction manager" in @TransactionConfiguration needed to be set also.

It is part of the spring-test lib.

Wintermute
  • 1,521
  • 1
  • 13
  • 34
  • Means I don't need savepoint mechanism here ? and current inserted data only removed or all ? – user3145373 ツ Apr 08 '14 at 08:12
  • A transaction is opened before every test. Then you perform some database changes (insert, update, delete) within the tests. Instead of committing the changes (making them persistent in the database), spring will rollback your changes (defaultRollback = true). So after the test, the database is in the state that it was before the test. – Wintermute Apr 08 '14 at 08:16
  • I am using this feature heavily in my tests. If you need some data for every test, you can add the data in an @Before-annotated method, or you simply have your postgresql already prefilled. – Wintermute Apr 08 '14 at 08:18
  • Right now I am setting data in my add() method, so is there can be problem or not ? or I ll have to set data in setUp() or anything else ? – user3145373 ツ Apr 08 '14 at 08:24
  • Sir I have added annotation `@TransactionConfiguration(defaultRollback = true)` after `@ContextConfiguration` data is added in DB but not destroyed.Shall I have to put @Before on add() method ? – user3145373 ツ Apr 08 '14 at 08:32
  • Hello Sir, have solved the problem I have to add `transacionManager="context bean transaction manager"` in `@TransactionalConfiguration` annotation. Thank You so much... – user3145373 ツ Apr 08 '14 at 08:56
  • Oh, ok. Thats interesting. I will add that to the answer. Nice to hear it is working. – Wintermute Apr 08 '14 at 09:52