JUnit と tycho-surefire-plugin を使用して OSGi サービスをテストしようとしました。
プラグインの構成
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<showEclipseLog>true</showEclipseLog>
<dependencies>
<dependency>
<type>p2-installable-unit</type>
<artifactId>org.eclipse.equinox.ds</artifactId>
</dependency>
</dependencies>
</configuration>
</plugin>
テストケース (ロギングステートメントなどは省略)。テスト クラスは、テスト対象のコードとは別に、独自の OSGi バンドルに含まれています。
@Component(name = "LdapConnectionConfigurationServiceTest", immediate = true)
public class LdapConnectionConfigurationServiceTest {
private LdapConnectionConfiguration testObject;
@Reference
public void bindTestObject(final LdapConnectionConfiguration testObject) {
this.testObject = testObject;
}
public void unbindTestObject(final LdapConnectionConfiguration testObject) {
this.testObject = null;
}
@Test
public void testLdapPort() {
assertEquals(10389, testObject.getLdapPort());
}
}
Tycho は、テスト バンドルである OSGi コンテナーを開始し、LdapConnectionConfigurationServiceTest
サービスを開始して、testObject を適切に挿入します。
その後、JUnit はこのテスト ケースを実行しますが、このクラスの別のインスタンスを作成します。testObject が注入されないため、NullPointerExceptions が発生します。
何が欠けているのかわからない... 私が欲しいのは、OSGi フレームワークによって提供される挿入されたサービスに対してテスト ケースを実行することです。