How can I test a service provider implementation module with Junit 5?

前端 未结 2 1637
名媛妹妹
名媛妹妹 2021-01-25 05:16

This is my base module which needs implementations of interfaces defined in myspi package. Various providers can offer MyProvider implementations. Base module uses them via mysp

2条回答
  •  Happy的楠姐
    2021-01-25 06:01

    SOLVED!

    I've removed the Junit from class-path to module-path and also removed all Junit 4 compatibility stuff such as RunWith() etc, and made my test pure Junit 5 test.

    I've added a module-info.java (Junit 5 doesn't require an open module although the books tell the opposite)

    After I've modularized the tests I found that it still doesn't execute the ServiceLoader stuff. Then I've started looking for the fault myself.

    And I found it! Running the ServiceLoader stuff in base module was possible, because the base module refers to the exported myProvider.jar, which in turns access a myProvider-config.properties file in the same directory. Without this config file myProvider cannot work properly.

    The problematic test module on the other hand, refered the eclipse project of the myProvider instead of its exported .jar file and hence could not find its config file and exits. I'd moved this config file from Netbeans to Eclipse simply copying it into the same directory. Thus missing config file was the problem.

    Changing the project settings I could run the tests without any failure.

    I would like to thank all the contributors who responded.

提交回复
热议问题