1

Spring Boot Application - MySQL - JUnit - Jenkins - Maven - Maven Sql Plugin

I currently use the Maven Sql Plugin to reset the Database Structure (structure only) on my Testserver, before every testrun. Every Test uses @Sql("script") annotation to refresh the data it needs (data only).

Lets say i have a table with userdata and many of my tests depend on the userdata. Now in development i add an column to that table. I now need to edit ALL scripts which affect userdata, because spring would check my datalayer and tell me a column is missing.

I wonder if there is a nicer, much cleaner approach to refresh the testdata, without adding the new column in dozen of scripts?

How do you manage testdata? Any experience with that?

Thanks in advance!

Sunchezz
  • 740
  • 6
  • 21
  • 1
    Have you looked at database versioning tools like Flyway? – jbx Oct 25 '18 at 12:49
  • No i did not know this tool. it looks promising. But as far as i can see, it only manages database structure right? in other words i can restore structure like in git?! What about the data insert querys? – Sunchezz Oct 25 '18 at 12:59
  • You should use JPA to persist data and create the precondition for your tests – Sándor Juhos Oct 25 '18 at 13:32
  • So the way it works is that you create separate SQL scripts in a resource directory (which you also track with git along with all your source code changes). With each new change you create a new file (the filename convention includes the version). It creates some extra meta tables in your database so it will detect that the database is out of date and automatically applies only the scripts that are missing. You can put various other things in it like repeatable scripts to execute, and they could have inserts no problem. – jbx Oct 25 '18 at 13:34
  • ok, but what about the 150 already existing scripts, which contain testdata (as INSERT querys). How do that queries get the change? – Sunchezz Oct 25 '18 at 13:44
  • @SándorJuhos how can JPA help me to create the precondition? I am already using springs extensions of jpa repositories. – Sunchezz Oct 25 '18 at 13:48
  • I mean for example if you have User entities, you should create them as Java objects and persist them with JPA repositories to create the precondition. In this way, if your domain model changing you can easily update your data loaders for your tests. – Sándor Juhos Oct 26 '18 at 12:41
  • But it still not the best way to write a data loader, because in this case, you can still load a data which is business logically inconsistent. In my opinion, the best way is using your application API to load data for your tests. – Sándor Juhos Oct 26 '18 at 12:54
  • @SándorJuhos Ok, for some lightweight UnitTests i would agree. JPA data insertion is easy for some entries. But for end to end tests, where i need a lot of complex data? For example i need a big tree structure, where it is not enough to test with 4 or 5 nodes, but with over 600. I dont want to write Tests, which include jpa, or api loading for this many data. Would you? Is this the best way to do it? – Sunchezz Oct 29 '18 at 08:04
  • 1
    I think yes. You only this way can guarantee the domain data integrity. I think with Java objects you can easier create complex data hierarchy than SQL inserts. In our current project, we have more than 2500 integration tests, and it is working correctly. – Sándor Juhos Oct 29 '18 at 13:43

0 Answers0