1

I have a module that contains package com.temp which has interface and implementation of th service - ServiceInterface and ServiceImpl. And I have in my module-info:

module temp {
    exports com.temp;
    provides com.temp.ServiceInterface with com.temp.ServiceImpl;
}

This is piece of my pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.0</version>
    <executions>
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <skipTests>${skip.integration.tests}</skipTests>
            </configuration>
        </execution>
    </executions>
</plugin>

And this is my TempIT

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class TempIT {

    @Test
    void tempTest() {
      System.out.println("JDKModulePath:" + System.getProperty("jdk.module.path")); //LINE Y
       ServiceLoader<ServiceInterface> loader = ServiceLoader.load(ServiceInterface.class);
       System.out.println(loader.findFirst().get().getString());//LINE X
    }

}

LINE Y prints: JDKModulePath:null. At LINE X I get java.util.NoSuchElementException: No value present.

How to make integration test of JPMS service? Is it possible to do outside the module in order to check module as one whole but without creating additional testing module?

EDIT 1: These are implementation and interface:

package com.temp;
public class ServiceImpl implements ServiceInterface {

    @Override
    public String getString() {
        return "This is test string";
    }
}

package com.temp;
public interface ServiceInterface {

    public String getString();
}
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • Please show the code of the ServiceInterface implementation and of course the interface would be helpful as well... – khmarbaise Sep 13 '18 at 13:37
  • @khmarbaise I edited the question. – Pavel_K Sep 13 '18 at 13:40
  • Have you correctly create a file in `src/main/resources/META-INF/services/com.example.CodecFactory` ? Which is needed for `ServiceLoader`? – khmarbaise Sep 13 '18 at 13:49
  • 1
    @khmarbaise as far as I know when JPMS services are used we don't create any files in meta-inf as all services are controlled by JPMS. – Pavel_K Sep 13 '18 at 13:52
  • @khmarbaise I am afraid I can't agree with you. ServiceLoader appeared before JPMS and can be configured via files in meta-inf. However, if we create these files we get out of the JPMS. – Pavel_K Sep 13 '18 at 14:03
  • https://stackoverflow.com/questions/52162778/how-can-i-test-a-service-provider-implementation-module-with-junit-5/52168316#52168316 helps? – Naman Sep 13 '18 at 14:04
  • The ServiceLoader is not related to JPMS cause its a mechanism load implementations during runtime which needs some resource to define what the implementation is...see the docs... (Typo in my first comment)... – khmarbaise Sep 13 '18 at 14:12
  • 1
    @khmarbaise When using modules you declare services differently—by using `provides` and `uses` directives in `module-info`. You still need `META-INF/services` files if you _aren't_ using modules, however. The documentation of `ServiceLoader` was updated in Java 9 to cover this. – Slaw Sep 13 '18 at 14:21
  • Can you modify the test to print out the value of the "jdk.module.path" system property? I suspect the issue is that the service provider is not on the module path. – Alan Bateman Sep 14 '18 at 06:39
  • @AlanBateman I edited the question, see class TempIT. – Pavel_K Sep 14 '18 at 09:23
  • 3
    If I read this correctly, the module path is empty. I don't know the maven-failsafe-plugin but it suggests it has not set the module path, it may be putting everything on the class path but is still missing the service provider. Someone familar with the maven-failsafe-plugin may be also to suggest debugging options to figure out what it is doing. – Alan Bateman Sep 14 '18 at 09:31

1 Answers1

3

It seems to be a bug, as failsafe doesn't put the module on module path. See the issue https://issues.apache.org/jira/browse/SUREFIRE-1570

Pavel_K
  • 10,748
  • 13
  • 73
  • 186