Proper way to test using ebean-test when class has dependencies #3244
Replies: 4 comments 4 replies
-
The above is not clear in that there is no reference to ebean Database or DB in the above. Maybe git clone the Spring boot example project and submit a PR showing the issue? https://github.com/ebean-orm-examples/example-springboot/blob/master/src/test/resources/application-test.properties |
Beta Was this translation helpful? Give feedback.
-
Yeah after some more investigation, this looks like a Database configuration issue on load. The test without the dependency is pointing to the correct docker database (it does not run the database config file), but the test with one is pointing to my local DB and reading like it is running the app (runs the database config file and has my current user and tenant providers). Is there a different way I need to configure the DB at first when the test has an autowired dependency? |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? |
Beta Was this translation helpful? Give feedback.
-
Figured this one out. Assuming your test profile is "test", place @Profile("!test") above your database configuration class. That ensures the default profile is not loaded which takes precedence over the ebean docker container running |
Beta Was this translation helpful? Give feedback.
-
I'm starting to go through my spring service and add tests. I have read the ebean docs referencing the new way to do testing via Docker containers. I was able to get some tests written for my Ebean classes and can confirm that it properly runs on the docker container.
However, I am trying to write tests for a @service within my Spring app and am trying to get it to run the tests on the docker container, but it is running them against my local database instead. I believe this may have to do with how Spring auto wire works alongside @SpringBootTest
Example:
When I run this test individually not through the mvn run tests command, the test runs fine and does not add entries into my local database. Is there a way to configure the mvn command to point to the spun up docker container?
Also side question. Whenever I use UserContext within this test, the insertions fail saying the created_by cannot be null. Is there a way you recommend setting up dependency tests like this so that Spring autowire does not interfere?
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions