Closed wbiller closed 8 months ago
That's a nifty trick, I didn't know about that one.
Cleaning up state is something that becomes a necessity quite soon once you start more sophisticated tests. I'm a little torn here between keeping things simple, YAGNI and preventing that others will run into this issue. After all, right now this codebase doesn't need a clean up between the tests. However, if you don't know what you're looking for, not having the clean up can lead to confusing and frustrating debugging.
I don't know if I'm too happy with having a dedicated .sql file that I'll run before every test. For two reasons:
repo.deleteAll()
?Mostly it's a matter of style. If you're working a lot with SQL anyways (and you're proficient with it) I don't see any big issues here. If you're more the person to drive everything through your OR-Mapper, it might be easier to just @Autowire
the correct repositories and go with it.
The thing with the single SQL file is just a personal preference of mine, and yes I always forget to add new tables to the file :D .
To make sure you don't run into any trouble (e.g. a corrupt state) it's always a good idea to remove any existing data before integration tests and e2e tests are executed.
From my experience it's a bit frustrating to do this in every test and for every entity that is handled by the tests, as you have to autowire each Repository and call the
deleteAll()
method. Fortunately there's SQL and Spring provides the@org.springframework.test.context.jdbc.Sql
annotation, which allows you to execute SQL before a test.So you can create a file containing nothing else than
delete
statements and have this executed before each test.delete-all.sql
SomeIntegrationTest.java