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

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

```

How to solve this problem?

Answer: 
If you are using Spring, I found this [blog post](https://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-resetting-auto-increment-columns-before-each-test-method/) which suggests using `@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)`

So that each test uses a new application context, thereby resetting the db increments.
Execute an SQL-script on your table:

```
TRUNCATE TABLE my_table RESTART IDENTITY;
ALTER SEQUENCE my_table_id_seq RESTART WITH 1;

```

H2 should be of version 1.4.200 or higher.

See <https://www.h2database.com/html/commands.html>