Problems testing services created with maven-scr-plugin when using pax-exam - java

I've set up a very simple "HelloWorld" service to demonstrate my problem. It uses the maven-scr-plugin to generate a service descriptor and has a pax-exam unit test. But when I try to run 'mvn clean test' it blocks for a while before giving me this error:
org.ops4j.pax.swissbox.tracker.ServiceLookupException: gave up waiting for service com.liveops.examples.osgi.helloworld.HelloWorldService
If I run 'mvn -DskipTests=true package' and then run 'mvn test' (without clean)
it works. The difference seems to be the addition of this line in my META-INF/M
ANIFEST.MF file:
Service-Component: OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml
Does anyone know if there is a way to make sure this line is added earlier in the build process so that 'mvn clean test' will pass? Or is there something else I might be doing wrong?
For reference, here is the pom.xml, the service, and the unit test.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liveops.examples</groupId>
<artifactId>HelloWorldService</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-aether</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.0</version>
</dependency>
</dependencies>
<properties>
<namespace>com.liveops.examples.osgi.helloworld</namespace>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<!--
| the following instructions build a simple set of public/private classes into an OSGi bundle
-->
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<!-- Bundle-Activator>${namespace}.internal.HelloActivator</Bundle-Activator -->
<!--
| assume public classes are in the top package, and private classes are under ".internal"
-->
<Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package>
<Private-Package>${namespace}.internal.*</Private-Package>
<!--
| each module can override these defaults in their osgi.bnd file
-->
<!--_include>-osgi.bnd</_include-->
</instructions>
</configuration>
<executions>
<execution>
<id>generate-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.9.0</version>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<generateAccessors>true</generateAccessors>
<strictMode>true</strictMode>
<specVersion>1.1</specVersion>
<outputDirectory>target/classes</outputDirectory>
</configuration>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
HelloWorld Implementation class
package com.liveops.examples.osgi.helloworld.internal;
import com.liveops.examples.osgi.helloworld.HelloWorldService;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Component;
#Component
#Service(HelloWorldService.class)
public class HelloImpl implements HelloWorldService
{
public String helloWorld(String personalization)
{
return "Hello " + personalization + "!";
}
}
The unit test
package com.liveops.examples.osgi.helloworld.internal;
import com.liveops.examples.osgi.helloworld.HelloWorldService;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.util.PathUtils;
import javax.inject.Inject;
import static org.ops4j.pax.exam.CoreOptions.*;
#RunWith(PaxExam.class)
public class HelloImplTest
{
#Inject
HelloWorldService hws;
#Configuration
public static Option[] configuration() throws Exception{
return options(
systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("WARN"),
mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.6.2"),
bundle("reference:file:" + PathUtils.getBaseDir() + "/target/classes"),
junitBundles());
}
#Test
public void testInjection()
{
Assert.assertNotNull(hws);
}
#Test
public void testHelloWorld() throws Exception
{
Assert.assertNotNull(hws);
Assert.assertEquals("Hello UnitTest!", hws.helloWorld("UnitTest"));
}
}

Use the ProbeBuilder to enhance your tested bundle:
#ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
probe.setHeader("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml");
return probe;
}
This most likely is all that's missing.
EDIT:
just in case you're trying to use pax-exam in the same bundle you need to take certain actions in your configuration method:
streamBundle(bundle()
.add(SomceClass.class).add("OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml", new File("src/main/resources/OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml")
.toURL())
.set("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml")
.build()).start()
a complete sample can be found at here

I may have a fix for this, though it seems a bit in-elegant. I can explicitly add the Service-Component to the maven-bundle-plugin section of the pom file.
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package>
<Private-Package>${namespace}.internal.*</Private-Package>
<!--Explicitly add the components no that they can be found in the test phase -->
<Service-Component>OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml</Service-Component>
</instructions>
</configuration>
<executions>
<execution>
<id>generate-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
Please let me know if anyone can think of a better way.

The Maven SCR Plugin only generates the service component descriptors but it does not include them in the manifest automatically.
So there's nothing inelegant about including a <Service-Component> instruction in the Maven Bundle Plugin configuration, that's just the documented usage.
Since the manifest header is missing, SCR does not register any services on behalf of your bundle, and that's why Pax Exam can't find the required service.

Related

Spigot/Bungeecord Hibernate - Invalid logger interface org.hibernate.internal.CoreMessageLogger

I tried using Hibernate ORM to handle my BungeeCord database mappings. I've used it before and had no problems doing so.
Now I revisited Minecraft programming and could not get Hibernate running.
My pom.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jakobi</groupId>
<artifactId>sessionutil</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>oss.sonatype.org</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<repository>
<id>maven.enginehub.org</id>
<url>https://maven.enginehub.org/repo/</url>
</repository>
<repository>
<id>repo.spongepowered.org</id>
<url>https://repo.spongepowered.org/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<type>javadoc</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.2.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>clean install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
And my main is:
package io.jakobi.sessionutil;
import io.jakobi.sessionutil.database.entity.Action;
import io.jakobi.sessionutil.database.entity.Session;
import io.jakobi.sessionutil.database.repository.ActionRepository;
import io.jakobi.sessionutil.database.repository.SessionRepository;
import io.jakobi.sessionutil.listener.PlayerChatListener;
import io.jakobi.sessionutil.listener.PlayerJoinListener;
import io.jakobi.sessionutil.listener.PlayerQuitListener;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.internal.CoreMessageLogger;
import java.io.File;
public class SessionUtil extends Plugin {
private static final String HIBERNATE_CONFIG_FILE_NAME = "hibernate.cfg.xml";
private SessionFactory sessionFactory;
#Override
public void onEnable() {
sessionFactory = new Configuration()
.configure(new File(this.getDataFolder().getAbsolutePath() + "/" + HIBERNATE_CONFIG_FILE_NAME))
.addAnnotatedClass(Session.class)
.addAnnotatedClass(Action.class)
.buildSessionFactory();
ActionRepository actionRepository = new ActionRepository(sessionFactory);
SessionRepository sessionRepository = new SessionRepository(sessionFactory);
registerListener(this, actionRepository, sessionRepository);
}
#Override
public void onDisable() {
if (this.sessionFactory != null) {
this.sessionFactory.close();
}
}
private void registerListener(SessionUtil instance, ActionRepository actionRepository, SessionRepository sessionRepository) {
this.getProxy().getPluginManager().registerListener(instance, new PlayerJoinListener(sessionRepository));
this.getProxy().getPluginManager().registerListener(instance, new PlayerQuitListener(sessionRepository));
this.getProxy().getPluginManager().registerListener(instance, new PlayerChatListener(sessionRepository, actionRepository));
}
}
The issue that I am facing is that on startup, the following exception is thrown:
WARNING] Exception encountered when loading plugin: SessionUtil
java.lang.ExceptionInInitializerError
at io.jakobi.sessionutil.SessionUtil.onEnable(SessionUtil.java:27)
at net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:265)
at net.md_5.bungee.BungeeCord.start(BungeeCord.java:285)
at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:67)
at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15)
Caused by: java.lang.IllegalArgumentException: Invalid logger interface org.hibernate.internal.CoreMessageLogger (implementation not found in PluginClassloader(desc=PluginDescription(name=SessionUtil, main=io.jakobi.sessionutil.SessionUtil, version=1.0-SNAPSHOT, author=Lukas Jakobi <lukas#jakobi.io>, depends=[], softDepends=[], file=plugins/sessionutil-1.0-SNAPSHOT.jar, description=A utility plugin to manage user sessions, libraries=[])))
at org.jboss.logging.Logger$1.run(Logger.java:2556)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:29)
at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:25)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:106)
... 5 more
A quick google search didn't really help. I found a comment saying I have to initialize the logger with Hibernate.initalize(object). I could not get that to work either.
Thanks in advance for any help! :)
I don't know how class loading works in this "environment", but the hibernate-core.jar should contain a class called org.hibernate.internal.CoreMessageLogger_$logger, which is the logger implementation that apparently can't be found. Maybe the class loader is broken?
Your maven shade plugin contains the following:
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
This causes all the logging dependencies to be dropped while shading. Removing this line will fix the issue.

Can I use PowerMockito in Quarkus with JUnit5?

Im am currently building a Quarkus application and for unit testing I am using JUnit5. Currently I have no other option then using PowerMockito to mock my static functions but I cant seem to find the dependencies for it in my Quarkus project.
Does anyone know what the best set of dependecies should be used in a Quarkus app for PowerMockito with JUnit5?
I guess you don't need PowerMockito, just the capability to mock static methods. Since 2.7.x (released in 2017) Mockito community made an experimental library called mockito-inline for mocking static methods or final classes. Some features (e.g. static method mocking) have already merged into mockito-core.
Additionally Quarkus has extension for Mockito.
Here is a working example
pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.zforgo</groupId>
<artifactId>quarkus-mockito-static</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>2.6.1.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Some utility class with static method
package io.github.zforgo;
public class SomeStaticClass {
public static String getSome() {
return "foo";
}
}
Test class
package io.github.zforgo;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;
#QuarkusTest
public class SomeStaticClassTest {
#Test
void nonMocked() {
assertEquals("foo", SomeStaticClass.getSome(), "Something went wrong");
}
#Test
void mocked() {
// try-with-resources is recommended in case of scoped (temporary) mocking
try (var mocked = Mockito.mockStatic(SomeStaticClass.class)) {
mocked.when(SomeStaticClass::getSome).thenReturn("bar");
assertEquals("bar", SomeStaticClass.getSome(), "Something went wrong");
}
}
}

How to run jar file of a maven project generated after build?

I made a simple project with a few tests in it and I want to be able to launch tests on other computers. I built the project using Run As -> Maven build... -> Goals: package in Eclipse, and I found a jar file in target folder of the project. But when I try to run it in cmd using java -jar project.jar I get the following error:
Error: Main method not found in class com.example.TestPurchase, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
As far as I know, TestNG doesn't need any Main method, because of the annotations.
That brings me to some questions:
Is something wrong with how I build my project?
Did I understand the method of executing tests via jar file correct?
Do I even have to use that jar file? Because I can run tests from command line being in project folder using mvn test
Here is my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Sightsy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<selenium.version>3.12.0</selenium.version>
<testng.version>6.13.1</testng.version>
<javafaker.version>0.14</javafaker.version>
<guava.version>23.2-jre</guava.version>
<extentreports.version>3.0.7</extentreports.version>
<extenttestng.version>1.3.1</extenttestng.version>
<assertj.version>3.8.0</assertj.version>
<maven.compiler.version>3.7.0</maven.compiler.version>
<commons.version>3.7</commons.version>
<commons.io.version>2.6</commons.io.version>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>${javafaker.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>${extentreports.version}</version>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>testng-extentsreport</artifactId>
<version>${extenttestng.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.TestPurchase</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/suites/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
I'm not familiar with TestNG framework, but my understanding is as following:
When "mvn package" is executed, your project is "validated", "compiled" and then "packed". During compilation, your code is changed to bytecode and can be interpreted (executed) by java virtual machine (JVM). This compiled code has only your application (I assume, that this is an application that is executed on Java server).
During the "package" step, these compiled classes (without tests) are put together in jar file. This jar file should contain only your application (again, without tests) - this is what you want because your jar file is smaller and contain only what is really needed.
Now when "mvn test" is executed, classes with test code are compiled and then executed. TestNG add the main function "automatically" so JVM knows what to do. Please note that running again "mvn package" will still NOT include these tests in jar file.
To address your questions directly:
Ad 1. No
Ad 2. Yes, "mvn test" is the correct way.
Ad 3. No, you should not use jar file during testing. It is used later during deployment.
First of all, you must have a separate class with "main" method, where you will specify your xml suite files.
package com.example;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
public class MainClass {
public static void main(String[] args) {
TestNG testSuite = new TestNG();
List<String> suites = new ArrayList<String>();
suites.add("path_to_your_xml_suite_file_in_target_folder");
testSuite.setTestSuites(suites);
testSuite.run();
}
}
Then, you will need to specify this class with "main" method in configuration of maven-shade-plugin in pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.TestNGMainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Then, when you will execute mvn build package without performing tests, you will have a fully working executable jar file in target folder, which you can launch in command line using java -jar name_of_you_jar_file.jar. It will need drivers and xml suite files to work

How to run junit tests on a non java project

I have a project folder but it is not a java project. It is a maven project. I have written a junit test and it runs perfectly when running in the eclipse IDE but when I run the maven command mvn install, it seems to skip my junit tests. I have included the test file in src/test/java/ (the name of my test is AppTest.java) and the main .java file (with the main method) is in src/main/java/. I have noticed that the project I am currently working on is a maven project and not a maven java project. I have included a screen of my current folder structure:
folder structure
Maven test output <- should not build as I have a deliberate test that should fail
This is the POM. I have deleted/commented out some sensitive parts so the pom file may be syntactically wrong but the main plugins I use are there; tap4j, junit and surefire.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>integration-api-parent</artifactId>
<groupId>uk.gov.dwp</groupId>
<version>1.0.2</version>
</parent>
<artifactId>aa</artifactId>
<version>1.0.6</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>aa</finalName>
<plugins>
<!-- plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin>
<plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin-->
<plugin>
<!-- Plug-in utilised for the execution of the JMeter Integration Tests -->
<!-- These tests are executed against the nominated integration server where as -->
<!-- instance of AA exists -->
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.9.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreResultErrors>false</ignoreResultErrors>
<suppressJMeterOutput>false</suppressJMeterOutput>
<overrideRootLogLevel>INFO</overrideRootLogLevel>
</configuration>
</plugin>
<plugin>
<!-- Step to copy the latest plug-ins that form this build to the integration server -->
<!-- This is done using the SCP command via the ANT plug-in thus allowing it to execute on all platforms -->
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tap4j/tap4j -->
<dependency>
<groupId>org.tap4j</groupId>
<artifactId>tap4j</artifactId>
<version>4.2.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<!-- plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/cassandra-assembly.xml</descriptor>
<descriptor>src/main/assembly/devenv-assembly.xml</descriptor>
<descriptor>src/main/assembly/main-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<!-- plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<configuration>
<skipPoms>false</skipPoms>
<sourceDirectory>${project.basedir}/src/main/latex-templates</sourceDirectory>
<outputDirectory>${project.build.directory}/latex</outputDirectory>
</configuration>
</plugin-->
</plugins>
</build>
</project>
AppTest:
package AccessGateway;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import org.tap4j.consumer.TapConsumer;
import org.tap4j.consumer.TapConsumerFactory;
import org.tap4j.model.TestSet;
public class AppTest {
Practise prac;
final String DIRECTORY = "C:\\Users\\Hello\\Desktop\\";
#Test
public void testHeaderProcessor() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderProcessor.txt")));
}
#Test
public void testHeaderPortForward() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderPortForward.txt")));
}
#Test
public void catunittest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catunittest.txt")));
}
#Test
public void catunitcrowstest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catcrowd.txt")));
}
#Test
public void testCrowd() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPCrowd.txt")));
}
#Test
public void testADFS() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPADFSformat.txt")));
}
}
The problem is the packaging of your project which is pom
You can't execute Surefire on this kind of project.
Try adding surefire plugin. When i have tests in my app i always include it (works for junit as well as testng). Based on your logs i can see that you dont have it declared.
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>

Services not listed in Karaf, what might be the reason?

I'm using combination of R6 OSGi annotations from OSGi Alliance, maven and Apache felix maven-scr-plugin.
After writing a simple bundle I don't see any services inside it (using Karaf webconsole or service:list )
The same works with Low Level API via BundleContext where I manually register a service.
As far as I understand maven-scr-plugin generates for me manifest and component XML files in runtime.
In the code below I would expect service SimpleMathI would be registered in Karaf Service Registry:
Did i miss anything?
package test;
//notice i don't use apache.felix, since:
//"Starting with the R6 release of the OSGi Declarative Services and Metatype specification, the official annotations support the same
//features as the Apache Felix SCR annotations in a more elegant manner and even provide additional functionality."
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
#Component
public class TestClass implements SimpleMathI {
public TestClass() {
System.out.println("contructing TestClass");
}
#Activate
protected void activate(ComponentContext c, BundleContext b) {
System.out.println("activate testClass");
}
#Deactivate
protected void deactivate() {
System.out.println("de-activate testClass");
}
public void doSimpleAdd(int x, int y) {
System.out.println("Result(TestClass): " + (x + y));
}
public void doSimpleSubstract(int x, int y) {
System.out.println("Result(TestClass): " + (x - y));
}
}
here is my pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>DStestDS</artifactId>
<version>0.0.5</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.20.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<!-- official R6 osgi annotations -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Did you maybe forget to install the scr feature?
feature:install scr
Your pom also seems to be broken. You need to use the maven-bundle-plugin or the bnd-maven-plugin. If you use the OSGi spec DS annotations then the maven scr plugin is not needed.
This is what I use in my builds:
https://github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-ds/pom.xml#L107-L118
It creates bundles and also processes DS spec annotations.
After Christian's suggestion I have added maven-bundle-plugin to the pom.xml file and removed maven-scr-plugin, now the pom.xml looks like below:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>DStestDS</artifactId>
<version>0.0.10</version>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.0</version>
<extensions>true</extensions>
<configuration>
<obrRepository>NONE</obrRepository>
<instructions>
<_include>-bnd.bnd</_include>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
And here is the output from the console:
no services listed
So I can't understand when and why service gets registered? Why no #Activate method is called?
BTW, I don't get any compile errors, also I don't do "Export->Deployable plug-ins" project, I simply do mvn clean install take the output jar file and put into Karaf's delploy folder.
After generating a bundle file I looked inside it and found that META-INF\MANIFEST.MF looks like:
Manifest-Version: 1.0
Built-By: username
Build-Jdk: 1.7.0_79
Created-By: Apache Maven 3.3.9
Archiver-Version: Plexus Archiver
it seems that something is missing, isn't it?
and I don't see any services listed via karaf webconsole:
karaf webconsole

Categories