[pax-exam] How to simulate beforeClass / afterClass hook *outside* of the OSGi container

154 views
Skip to first unread message

Nicolas Brasey

unread,
Aug 4, 2017, 5:59:06 AM8/4/17
to OPS4J
Hi,

Context: I want to use pax-exam for our business integration tests that needs to have a database (neo4j) that is not OSGi friendly running before the test are executed. we use maven.

Also, neo4j provides a embedded server that works extremely well outside of an OSGi container, but I can't find a way to start this embedded server with pax-exam outside of the container before the pax-exam machinery is starting.

The idea is to start the database in the non-OSGi context when the pax-runner is starting, something like the beforeClass, and stop the database after all the tests are finished ala afterClass.

Does anyone has an idea how to do this ?

Thanks
Nicolas

Toni Menzel

unread,
Aug 4, 2017, 6:47:06 AM8/4/17
to OPS4J Mailing Lists
Hey Nicolas,

I think you are looking at something like the (new) acceptance test api that runs from outside of 

This is currently in active development and is lacking some features like smooth access to OSGi Services from the outside.
Currently, only the rest client is available (using RestAssured), but I a new one making internal services available to the test automatically is already on my local branch.

In any way, we also could think of making out-of-container setup code available in regular "@RunWith(PaxExam.class)" tests. Did you try booting Neo4j inside the @Configuration method? that is executed before the OSGi container is launched, so it does run in plain java.

Another try: did you try using a Junit Rule with "@RunWith()"? this should work, too.

Toni

--
--
------------------
OPS4J - http://www.ops4j.org - op...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "OPS4J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ops4j+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nicolas Brasey

unread,
Aug 4, 2017, 8:39:14 AM8/4/17
to op...@googlegroups.com
Hi Tony,

Thanks a lot for your answer.

I think what you are working on is absolutely great, and I think it would also perfectly fit for our needs. Starting the OSGi container from a junit rule makes the tests cleaner. I guess this would also work with Karaf, right ? But, just for the curiosity, how do you plan to access the OSGi service from outside the container ? And when do you plan to push your local branch ? :-)
 
2) Yes I tried booting neo4j from the @Configuration method, but there 2 issues IMHO:
a) It provides only a way to start neo4j, obviously no hook to run some post tests code
b) since the configure code is also deployed as an OSGi bundle, it forces you to make sure the <Import-Package> does not include the neo4j packages, which cannot be resolved. It is feasible, but this is hard work and I'm not sure about the maintainability of this over the time

3) Not sure I understood your proposal with the RunWith. Do you mean implementing my own Junit runner that wraps the PaxExam runner, and run my tests with it ?


Cheers,
Nicolas
 

You received this message because you are subscribed to a topic in the Google Groups "OPS4J" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ops4j/RlgwSX04-O8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ops4j+unsubscribe@googlegroups.com.

Toni Menzel

unread,
Aug 4, 2017, 9:39:47 AM8/4/17
to OPS4J Mailing Lists
So, about the imports: yes, well there used to be an option to let you mark all generated imports to be optional for the probe. Have to look at this.

About the Acceptance Driver: yes it will work with any Pax Exam Test Container. Stay tuned.

About the pre/post hooks: can you try implementing a Junit Rule? https://github.com/junit-team/junit4/wiki/Rules (see ExternalResourcesRule). This will give you the right amount of control.
But, I guess you will still get superfluous (and unresolvable) imports for that even though they will not be really executed in-container.
Need to update myself how to overwrite that..

Nicolas Brasey

unread,
Aug 4, 2017, 10:43:50 AM8/4/17
to op...@googlegroups.com
On Fri, Aug 4, 2017 at 3:39 PM, Toni Menzel <toni....@rebaze.com> wrote:
So, about the imports: yes, well there used to be an option to let you mark all generated imports to be optional for the probe. Have to look at this.


Ok I will look into this option, thanks for the tips.

 
About the Acceptance Driver: yes it will work with any Pax Exam Test Container. Stay tuned.

Amazing, looking forward!

 
About the pre/post hooks: can you try implementing a Junit Rule? https://github.com/junit-team/junit4/wiki/Rules (see ExternalResourcesRule). This will give you the right amount of control.
But, I guess you will still get superfluous (and unresolvable) imports for that even though they will not be really executed in-container.
Need to update myself how to overwrite that..

So, do you mean the Junit rule as pre/post test is executed outside of the container ? I thought it was executed in the container....

Christoph Läubrich

unread,
Aug 5, 2017, 7:50:50 AM8/5/17
to op...@googlegroups.com
Even though neo4j is not "osgi-friendly" (what ever that means ;-) you can create abundle that embedds all that neo4j requires, add an Activator that starts up your db and provide a custom service to trigger "after-test-checks" in whatever way you need. In your test you can the collect the service to

a) make sure the db is up and running
b) fire your checks whenever needed
--
--
------------------
OPS4J - http://www.ops4j.org - op...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "OPS4J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ops4j+un...@googlegroups.com.

Niclas Hedhman

unread,
Aug 5, 2017, 10:19:11 PM8/5/17
to OPS4J

I thought I should chip in and point out that Junit 5 ahs received the entire extension mechanism, and both Rules and RunWith constructs are deprecated, and implemented using the new extension system. 

Perhaps it would make more sense for new functionality in Pax Exam to look forward to JUnit 5.

Just my 2 sen.

Nicolas Brasey

unread,
Aug 7, 2017, 5:14:58 AM8/7/17
to op...@googlegroups.com
Hi Christoph,

Yes, that is exactly what I tried already, to bundle neo4j and all the dependencies with the maven shade plugin (like the ones in service mix bundles). I realized that this is not really ideal for the following reasons:

1) The build time exploded, to create this huge jar takes more than 1minutes and 30 secs
2) The pom.xml is far from being straight forward to write, you need to have a good understanding of the internal structure of neo4j in order to maintain it. Also, I'm afraid this is time consuming on the long term and will requires tricky changes for each major release of neo4j.
3) It was not even working! I had issues with neo4j internal way of loading modules with the java 6 ServiceLoader mechanism, which did not find http services for reasons I still ignore...

Anyway, I finally gave up fighting with it ;-)



  

To unsubscribe from this group and stop receiving emails from it, send an email to ops4j+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
--
------------------
OPS4J - http://www.ops4j.org - op...@googlegroups.com

---
You received this message because you are subscribed to a topic in the Google Groups "OPS4J" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ops4j/RlgwSX04-O8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ops4j+unsubscribe@googlegroups.com.

Achim Nierbeck

unread,
Aug 8, 2017, 2:29:06 AM8/8/17
to op...@googlegroups.com
Hi, 

just one question came to my mind, regarding your scenario. 
For your Integration tests you don't seem to rely on testing the internals of the OSGi container, right? 
In that case how about starting the Pax Exam tests as server tests, that way you only need to call externally available interfaces and can make sure you have neo4j started prior to that. 

regards, Achim 


Apache Member
Apache Karaf <http://karaf.apache.org/> Committer & PMC
OPS4J Pax Web <http://wiki.ops4j.org/display/paxweb/Pax+Web/> Committer & Project Lead
blog <http://notizblog.nierbeck.de/>
Co-Author of Apache Karaf Cookbook <http://bit.ly/1ps9rkS>

Software Architect / Project Manager / Scrum Master 

Toni Menzel

unread,
Aug 8, 2017, 3:38:28 AM8/8/17
to OPS4J Mailing Lists
There is already work on this for Pax Exam 5. No worries.

Nicolas Brasey

unread,
Aug 8, 2017, 3:38:58 AM8/8/17
to op...@googlegroups.com
Hi Achim,

No, I'm not so much interested to test the OSGi mechanics, I want to have high level business tests on our business layer which is a set of OSGi services. This requires the database and the messaging system to run as well. 

In fact, our app provides only rest api's from outside, and we have already a set of mocha integration tests. Mocha is fine for testing rest message structure and non-regression at the level but a little cumbersome and hard to maintain when it comes to test more complicated business scenarios. That is why I'm looking for a Java API in order to implement those tests, pax-exam is ideal for me in that context.

Cheers,
Nicolas


    

Toni Menzel

unread,
Aug 8, 2017, 3:53:34 AM8/8/17
to OPS4J Mailing Lists
@Nicolas, I think your use case is a prime example out-of-container tests that will become more accessible with Exam 5. (Acceptance Test API). 
Here's the status:
  • There is a way to bootstrap a Test Container using the typical Exam Option DSL (done)
  • There is a very simple Rest Client API based on Rest Assured. That is really minimal and I am not sure if we just let Rest Assured do the job or keep being it an implementation detail.
    • Why keep it as an implementation detail? Rest Assured is such a broad API, confusing at times, many ways to do the same thing. For example, there is a java object mapper but also groovy expression language. Two approaches, to do the same thing: evaluate HTTP responses.
    • Why expose Rest Assured? It's already a broad DSL that some people may already be familiar with. And building abstractions over abstractions is an anti-pattern. Pax Exam should not fall into this trap.
  • There another client that makes OSGi Services available to Acceptance Tests via Tunneling the request to the container. The tunneling part is important because I want any OSGi service to be accessible, not just the ones designed for serialization. (not safe in all cases but better than no access at all) (in progress)
Once this is done, we will look forward to a Pax Exam 5.0 Milestone 1 release. (Christoph, there is still a lot to be done, even breaking changes.But I'd like to see early feedback on the many areas Exam 5 brings. WDYT?) 

Toni

Achim Nierbeck

unread,
Aug 8, 2017, 3:54:57 AM8/8/17
to op...@googlegroups.com
Hi Nicolas, 


@toni, it's already available ;) 

regards, Achim 

Toni Menzel

unread,
Aug 8, 2017, 4:00:27 AM8/8/17
to OPS4J Mailing Lists
This is the very simple server mode (i did that back then). If it is enough for you, sure. No big deal. You don't get any lifecycle integration with your test framework and you need to do the configuration for accessing your server yourself (sure, if its only rest, you have all the tools of the world)

Nicolas Brasey

unread,
Aug 8, 2017, 4:15:54 AM8/8/17
to op...@googlegroups.com
The server mode is not enough for me, as I would like to be able to write tests against the OSGi services directly, I don't want to use the external REST api.

Out of curiosity, and not knowing how exactly pax-exam works in details, what is the reason the BeforeClass and AfterClass are not implemented ? 

For me, the "simplest" would be to introduce certain hooks, for example:

a) BeforeClass (called once before the tests are executed, outside of the container)
b) Probe bundle Activate (called once before the tests are executed, inside of the container)
c) Probe bundle Deactivate (called once after the tests, inside the container)
d) AfterClass (called once after the tests are executed, outside of the container)

Maybe the same for the before and after hooks. One hook called from outside, one from inside.

My 2 cents...

Nicolas



Christoph Läubrich

unread,
Aug 8, 2017, 2:19:49 PM8/8/17
to op...@googlegroups.com
Hi Toni,

If i see right there is some kind of snapshot repro for OPS4j maybe that
would be an Option? I'm not very familar with this I just have seen that
when realeasing pax-wicket.
If thats not an option we might provide some kind of preview1 ... x
Versions?

Toni Menzel

unread,
Aug 8, 2017, 6:37:34 PM8/8/17
to OPS4J Mailing Lists
yes, our ci.ops4j.org deploys successful builds to oss.sonatype.org/content/repositories/ops4j-snapshots automatically.

Christoph Läubrich

unread,
Aug 9, 2017, 1:13:02 PM8/9/17
to op...@googlegroups.com
Ah... okay, then we should make sure the build passes and convince people to test new features from the current Snapshot...

Anyone interested in testing the EclipseContainer with some real-life apps? :-)
Reply all
Reply to author
Forward
0 new messages