JUnit4OSGI with Equinox - junit

I wonder if someone can point me to a good tutorial/sample/best practices about using JUnit4OSGI with Equinox as the OSGI container?
I already downloaded JUnit4OSGI and started writing sample test cases, but what I'm looking for are answers for questions like:
1- How to structure the unit tests? put it in a separate bundle or each bundle should have its own unit tests?
2- How to run the unit tests using a maven plug-in? Or is there a better way?
3- How to run the unit tests during development as I understand there is a command line specific to Felix only.
Thanks.

I cannot help with Junit4OSGi but I can offer an alternative that works with Maven. It has the following parts:
testRunner: A module that can run tests if they are registered as OSGi services
testRunner-junit4: Junit4 engine for the testrunner module
eosgi-maven-plugin: A maven plugin that can use the modules above and run the unit tests during integration-test phase in equinox, felix or a custom container
richconsole: A simple module that makes it possible to drop modules to the OSGi container. Together with the maven plugin they can be used to update maven dependencies in the container without restarting it
Testrunner itself does not really care what technology offered the OSGi service. It could be blueprint, DS, iPojo or whatever. It needs the followings:
eosgi.testId service property must be provided. This is a unique value
eosgi.testEngine service property must be provided. In case of JUnit this is junit4
In case of JUnit, the class/interface must be annotated with JUnit annotations. Beware that blueprint wraps the service objects (I do not know if iPojo does it as well) so you have to implement an interface in your class and annotate the functions in the interface instead of the class
I think the best place to start is the maven usage site. At the moment the osgi-liquibase-bundle project uses the most up-to-date versions. Without knowing what it does, it is a good example of configuration (project hierarchy and pom.xml configuration).
All the modules and the plugin is available at maven-central.
Update
A guide about the usage of the tools is available at http://cookbook.everit.org
Update 2016.12
There is an Eclipse plugin now that allows the user to start/stop/debug/upgrade the OSGi environments specified by eosgi-maven-plugin. The usage of the plugin has benefits over the usage of richconsole.
Cookbook will be updated soon to use the Eclipse plugin instead of richconsole.

Related

How to enforce module boundaries in Java <= 8

I want to modularize a monolithic application by using Java modules (introduced by Project Jigsaw).
Unfortunately I'm currently stuck to use Java 8. I would like to build those modules (jar files) anyway, but without the feature of using a module-info.java file for declaring the dependencies and the exported API.
Some weeks ago I stumbled upon an API/framework which allows to define nearly the same things that you can do in a module-info.java file. It could be used in JUnit tests in order to enforce module's not to use the internal's of another module and that module's only can access modules they explicitly depend on.
Of course you do not have any assistence from the compiler or the IDE, but you can enforce the access by JUnit. Unfortunately I do not find the project which provides the API any more.
Can anyone help?
Btw. if there is another good approach beside a unit test, please let me know, too!
https://www.archunit.org/ is what I have been looking for.

Jruby rails - where should the java code go?

In my rails application I need to write some custom Java classes, so just wondering if there's any guidelines/best practice on how to structure the project repository? I mean like rails have a default directory structure in place, like app, models etc..., what should be the best place for my custom Java code?
I don't think there's a standard recommended practice, but there are a few options:
ruby-maven provides a DSL and a set of Rake plugins to build Java code the Maven way.
jruby-gradle-plugin provides similar support for the Gradle build tool.
For managing dependency libraries, there's also jar-dependencies.
If you're going to start writing Java/JVM code to go with your app, you'll probably need to integrate with the rest of the Java ecosystem at some point, and these tools are designed to support that.

Configuring system tests in junit

We currently have a dedicated tool for running system tests on our web services, but I've been thinking of re-writing it to be hostable within jUnit4.
This would give us several advantages, including the full power of Java to set up and assert results, as well as hopefully a simpler method of running the tests (both from CI and the IDE).
However, the tests would need a URL configured for it to test against (it is currently too impractical to initialise a local servlet for testing). Given this, is it still a good idea to try and host it in jUnit? If so, what's the best way to add the configuration?
It depends on how you will be running your unit tests. I normally use maven, then you have a test/resources directory where you can store the test setup.
You can use spring with AbstractDependencyInjectionSpringContextTests (which gives you spring-injected configuration, you can swap the main spring config for a test version)
Here is a dicussion about configuring junit in eclipse
If you're doing non-unit testing, TestNG might be a better option than JUnit. More specifically, data providers seem like they would be a good fit in the scenario you describe (example here). Data providers can retrieve their data pretty much from anywhere: a text/properties/xml/Excel file, a database, a remote host, etc...

Recompiling sources with a test specific jar

I have a multimodule project that is dependent on a rather large thirdparty library/apis. It is not possible to run automated junit tests with the actual library, since the apis interact with a system external to the management of the dev environment. As a workaround, we created classes that mocked the interfaces/apis so that we could do junits. We use eclipse as our IDE for development, so to run junits, we simply move the jar up higher in the classpath, recompile, run our junits, and everything works great.
Looking for a similar solution now that we are moving to Maven. Basically, after building our code, need to rebuild all of it again using the mock jar to run junit tests. This goes across all modules of the application. I tried adding the jar in the test scope, but that is only used for compiling the test classes; the sources classes are still compiled with the real jar. However, it fails at runtime since our mock jar does not match all signatures of the real jar (so a hot swap of the jar doesn't work; making all signatures match would be large undertaking). So the source needs to be recompiled with the jar.
So it appears there are two options:
1) Make a standalone test module that somehow pulls in all the source modules code and recompiles it using the mock jar and runs the tests or
2) Each module creates a testjar in addition to its installable jar that compiled with the mock jar, is then used by the dependent modules during the test phase.
Please advise. Any examples of how to do either of the above would be greatly appreciated.
Check out this link. I think it has what you need. You can handle this using two maven profiles. According to this you can have different dependencies for each profile.

How to configure properly IntelliJ IDEA for deployment of JBoss Seam project?

I would like to use IntelliJ IDEA for development of JBoss Seam project. seam-gen is creating the project stub, however the stub is not complete. In particular it is not clear how to deploy such project.
First of all I had to define manually web project facelet and add libraries to its deployment definition.
The other problem was persistence.xml file. In the Seam generated project it does not exists, since Ant is using one of the persistence-dev.xml, persistence-prod.xml, persistence-test.xml files, changing its name, depending on deployment type (which is ok). Obviously I can create persistence.xml by hand, but it goes againts Seam way of development.
Finally I decided to use directly ant, which is not partucularly comfortable.
All these tweaks made me think that I am doing something wrong from the IntelliJ IDEA point of view.
What is the efficient way of configuring IntelliJ for usage with JBoss Seam (deployment, in particular)?
I am using JBoss Seam 2.1.1, Intellij 8.1.4, JBoss 4.3.3
facet
I don't think you have to create any facet manually, as long as i remember IDEA always did it automatically for me...
persistence.xml
If IDEA didn't grab automatically persistence-dev.xml, you can tell her to use this file and not care that it's name will change after deployment...
artifact
You can still build your app using ant and create an analogical artifact in IDEA in the same location (for example exploded-archives/Project.war) - just leave "Build on make" unchecked (you want to build this artifact with ant)...
deployment
now, when you create new run configuration for the jboss as, in the deployment tab you can choose your artifact... just as if it was built by IDEA