質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top