Reputation: 267390
I have a maven project with IntelliJ, and I'm fairly new to both.
I have a spring mvc application running now with hibernate (and I'm exciting about it!).
Now, I'm coming from .net so with .net I had to write extra code to setup the sessionfactory etc. in my unit test project since it isn't like a web application where things get generated on first load and then on a per request basis also.
How can I do this with hibernate? I want to use mysql, but have it use a different database like: appname_test
BTW, when using a maven project, does IntelliJ internally use the maven commands to build and run tests?
Is it possible for me to isolate my tests and run a particular file using the maven commadn line?
Upvotes: 0
Views: 1347
Reputation: 1986
since you are using spring, you can configure another session factory using a different data source for tests and use that configuration file in test cases. Its all injected so you wont have to write an additional code, just some configuration.
Maven plugins do use the same commands as command line. Ultimately, how your tests will be run will depend on how you configure pom.xml. Assuming you are using surefire plugin for running tests, you can certainly define which class you want to use (suite?) although I prefer running all tests inside a folder.
Upvotes: 1
Reputation: 902
If you're using the hibernate.cfg.xml file to configure Hibernate, you'll want to put a copy of it in src/test/resources and change it to point to your junit database.
If you're using persistence.xml, you can either copy it into src/test/resources or add to the existing one. Either way, give it a unique persistence unit name and reference it by that name when you load it up at runtime.
I've always had to write code to fire up a SessionFactory/EntityManager in unit tests. I usually setup a base class that does that, and then extend it for every test that needs to use Hibernate.
Upvotes: 0
Reputation: 28991
In maven you have src/main/
for main code and scr/test/
for tests. You could have same files/classes in both, and during tests the latter has precedence.
It's unrelated to IDE, the IntelliJ just imports the maven's project. Meanwhile all maven projects could work independently from IDEs.
Upvotes: 1