• Post Reply Bookmark Topic Watch Topic
  • New Topic

Starting all infrastructure once for all test classes  RSS feed

 
P Marksson
Ranch Hand
Posts: 41
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each of my test class starts the infrastructure required to do the tests. These components consist of Elastic Search and a Jetty server. Having each test class start and stop these and also running all the test classes sequentially is not the most efficient thing to do, since it takes ages to complete the tests. Starting the infrastructure once -> Run all the test classes -> Stopping it would be much more efficient but not easily achieved.

The difficulties comes when you try to make it work in your IDEA and Maven(where we use failsafe plugin). Using shutdown hooks to close all the infrastructure down almost worked. Gitlab(it's like Jenkins I guess) did not like shutdown hooks and there are no guarantees they will run.
Since we use JUnit 4,I looked into JUnit Suites, which did not work in IDEA.

Some ideas?
 
Stephan van Hulst
Saloon Keeper
Posts: 7207
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do your unit tests require a Jetty server?

You should be mocking services that your classes need.
 
P Marksson
Ranch Hand
Posts: 41
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Why do your unit tests require a Jetty server?

You should be mocking services that your classes need.


Integration tests.
 
Rob Spoor
Sheriff
Posts: 20946
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not split it into three steps in Jenkins:
1) Start everything you need.
2) Run the integration tests.
3) Stop everything again.

That does mean that you can't run a single test in your local IDE without having the same (or at least a similar) setup. However, this is how we do it where I work and it works just fine.

Some notes:
- You shouldn't run integration tests when you simply do a mvn clean package. Put them in a different profile which you can then active in Jenkins (using -Pprofile). We have one profile for Eclipse that looks like this:

- You can use system properties with sensible defaults to use separate URLs for local testing and testing on Jenkins.
 
P Marksson
Ranch Hand
Posts: 41
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't have said that we only start Elastic Search and a Jetty server, because we start about 5 or 6 embedded servers and in future probably. Most of these use random ports which we access by a embeddedServerInstance.getPort() call.
This is just one reason why we can't do this with a Maven plugin/profile.

We are looking for something that would emulate a @BeforeAll or @BeforePackage that could exist in JUnit. We want to do this in code.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36659
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd do this in Maven (or Jenkins) too. If I absolutely had to do it in JUnit, I'd look at writing a custom test runner.
 
P Marksson
Ranch Hand
Posts: 41
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already tried that. Using a custom runner, you can't know when the last test have run, so you won't know when it's time to shut everything down.
 
Rob Spoor
Sheriff
Posts: 20946
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write your own test suite, but you'll have to make sure that Maven doesn't automatically pick up your tests but only your test suite. That will also make it just about impossible to run a single test.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36659
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JUnit 5 solves this problem. There are @BeforeAll and @AfterAll annotations so you  can run code at those points.

JUnit 5 is on Milestone 4 and releases late in the year.
 
P Marksson
Ranch Hand
Posts: 41
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:JUnit 5 solves this problem. There are @BeforeAll and @AfterAll annotations so you  can run code at those points.

JUnit 5 is on Milestone 4 and releases late in the year.


Not true. BeforeAll and AfterAll is just a renaming of BeforeClass and AfterClass.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36659
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. I need to start memorizing the mappings! Have a cow for the correction.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!