5

I have writting some JUnit integration tests that currently run from Maven (via the command line or out CI server). These integration tests automatically configure and startup the database and servlet container inside of Maven's 'pre-integration-test' lifecycle phase.

I would very much like to run these JUnit tests (like I do all our other tests) from within an Eclipse JUnit launch configuration. However, the JUnit launch configuration does not trigger the pre-integration-test executions and thus the environment to test is not properly established.

Whats the best way to get this to work?

Dave
  • 21,524
  • 28
  • 141
  • 221
  • Did you find a solution for your problem? I have exactly the same need. In fact, the only task that must be done during the pre-integration-test phase is to start a tomcat server and I have been doing this manually. So, if the server is running, I'm able to debug integration tests on Eclipse. – Gabriel Oliveira Jul 14 '15 at 12:21
  • Yes - we moved all our database set up and configuration code into some manager objects which can be configured via Spring then run when our system starts up. Then we added to every Junit file `@ContextConfiguration({ "/testWeb-applicationContext.xml" })` annotations so that the Spring application context would get launched before running any tests. Hope this helps. – Dave Jul 16 '15 at 19:03

4 Answers4

3

You can setup Maven Run Configurations to run within Eclipse, using Run -> Run Configurations, assuming you have the Maven plugin for Eclipse. Using the goals clean verify will run unit tests and integration tests.

Also if you use the failsafe reports Maven plugin, you can create JUnit style reports, that you can open in Eclipse using the JUnit framework, to see which tests passed and failed.

The Cat
  • 2,375
  • 6
  • 25
  • 37
  • Thats currently what we do, but its disconnected from the extremely handy JUnit GUI inside eclipse. Didn't know that failsafe reports can be opened in Eclipse in the JUnit GUI...will have to check that out. Thanks. – Dave Apr 15 '13 at 16:35
  • Turns out you can do that with Surefire's XML outputs as well. – Dave Apr 15 '13 at 17:18
2

One way to do this I just discovered is to use Eclipse Launch Groups as shown here:

http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.cdt.debug.application.doc%2Freference%2Fcdt_u_run_dbg_launch_group.htm

I missed this because this capability, while language independent needs to be installed from the C/C++ side of Eclipse as shown here:

What installable component provides 'launch groups' in Eclipse?

Within the launch group the first job should run a maven task to process test resources, the next jobs launch the server(s), and then finally a job to execute the junit tests.

Community
  • 1
  • 1
Dave
  • 21,524
  • 28
  • 141
  • 221
0

I don't know what database you want to use, but for unit tests its handy to use an in memory database and maybe also an embedded servlet container. Maybe you could use something like http://arquillian.org/

Wouter Schut
  • 907
  • 2
  • 10
  • 22
  • We use several databases depending on the Maven profile....all of which are automatically setup and configured via Maven during the pre-integration-test lifecycle phase. The question is how to get that work done via the JUNit Eclipse launch config if possible – Dave Apr 15 '13 at 16:33
-3

JUnit framework should be mainly used for Unit testing and not Integration testing. Integration testing could be performed by QA team.

If you have lot of Integration plumbing code then you should considering Mocking of external elements, such as Databases and web services calls.

Concentrate mainly on testing code that you have written.Add negative, positive and exceptional test scenarios. That will give your code more credibility than full integration test.

For Database I suggest using DBUNIT > http://www.dbunit.org/ which is an excellent tool for testing database plumbing. This tool can create XML schema from your database and you can keep data as xml replicating the database. This will avoid changing any dataset for your JUnit testing.

Venod
  • 1
  • I agree that Eclipse's JUnit capability is intended for Unit tests and not integration tests. Indeed we already have lots of unit tests in addition to the integration test this question is about. IMO, integration testing should be done by Maven as part of the build process (on a CI server) AND by developers who run the tests by hand within an IDE as a debugging tool. I'm just trying to make that experience simpler by using the JUnit GUI. We do this for other integration tests, but not ones that require `pre-integration-test` lifecycle phase. – Dave Apr 15 '13 at 17:12
  • 1
    I am sure you might have considered the JUnit Classpath setting , Environment and @Setup methods. Is the pre-integration-test phase is so complicated to fit in above places ? – Venod Apr 15 '13 at 19:30
  • It's a valid question, but yes, we have a TON of configuration into these Maven executions. I am thinking more and more that I'll just have to make a launcher for the Maven execution first, then the JUnit one second. http://stackoverflow.com/questions/4053265/eclipse-running-multiple-launch-configurations-at-once – Dave Apr 15 '13 at 19:39
  • Basic workflow? and adding workflows. Does that uses same JVM? I just downloaded the plugin and run. Good luck with that. – Venod Apr 15 '13 at 20:21
  • Answer and not avert the question. Junit in Eclipse can be used however the programmer sees fit. – Blessed Geek Dec 22 '16 at 02:28