Reset auto increment in h2

I am testing a controller that returns a json response, but the tests fail after the first time since the h2 database does not reset auto increment id. Using fixtures or creating objects manually has the same problem.

@Before
public void setUp() {
    Fixtures.deleteAllModels();
    Fixtures.loadModels("data.yaml");
}

How to solve this problem?

+5
source share
2 answers

Launch the playback application, launch the browser using this URL (if you are using the local playback application):

http://localhost:9000/@db

Type h2 db and enter the command below and run:

ALTER TABLE <table_name> ALTER COLUMN <column_name> RESTART WITH 1

If you want to do this programmatically, it Fixtures.executeSQL()may be useful

For more information, check out http://www.h2database.com/html/grammar.html#alter_table_alter

+12

Spring, , @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)

, , db.

+1

All Articles