Package org.iplass.mtp.test
Class MTPJUnitTestExtension
java.lang.Object
org.iplass.mtp.test.MTPJUnitTestExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback,org.junit.jupiter.api.extension.BeforeAllCallback,org.junit.jupiter.api.extension.Extension,org.junit.jupiter.api.extension.InvocationInterceptor
public class MTPJUnitTestExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.InvocationInterceptor
JUnitに単体テスト用のユーティリティ処理を組み込むための JUnit5 Extension です。
MTPJUnitTestExtension を組み込むことにより、容易な方法により、iPLAssの設定ファイルの指定、テナント、テストユーザーの設定、トランザクション制御を行うことが可能です。 設定方法は以下のいずれかの方法、またその組み合わせで行うことが可能です。
- プロパティファイルによる指定
- テスト全体にわたり設定が適用されます
- クラスレベルアノテーションによる指定
- 当該クラスに限り設定が適用されます。ネストクラスに同一設定が存在した場合は、ネストクラスの設定が優先されます。
- メソッドレベルアノテーションによる指定
- 当該メソッドに限り設定が適用されます
上記の指定方法は混在可能です。 指定の優先度は、
メソッドレベルアノテーション > クラスレベルアノテーション > プロパティファイル
となります。
プロパティファイルによる指定方法
プロパティファイルは、"/mtptest.properties"として、クラスパスからロードされます。 プロパティファイルをロードするパスを変更する場合は、システムプロパティ"mtp.test.config"にて指定可能です。 次の項目をプロパティファイルにて指定可能です。
| 項目名 | 説明 |
|---|---|
| configFileName | テスト実行時のiPLAssの設定ファイルのパスを指定します。 設定例: configFileName=/testsample/test-sample-service-config.xml |
| rollbackTransaction | テスト実行時のトランザクションをロールバックするか否かを指定します。 設定例: rollbackTransaction=true |
| tenantName | テスト実行時のテナント名を指定します。 設定例: tenantName=testDemo1 |
| userId | テスト実行時のユーザーのaccountIdを指定します。 設定例: userId=testUser |
| password | テスト実行時のユーザーのパスワードを指定します。 設定例: password=testUserPass |
クラスレベルアノテーションによる指定、及びメソッドレベルアノテーションによる指定方法
アノテーションにより、クラス単位、メソッド単位でテナント、ユーザーなどの指定が可能です。 指定可能なアノテーションは以下です。
以下にアノテーション記述されたテストクラスの例を示します。
//アノテーションで指定されていない設定はプロパティファイルの設定値が適用されます。
@TenantName("testDemo1")
@AuthUser(userId="testuser", password="testtest")
@ExtendWith( MTPJUnitTestExtension.class )
class SampleJavaTest {
@org.junit.jupiter.api.Test
public void testDemo1Tenant() throws IOException {
//do test...
:
:
}
@TenantName("testDemo2")
@AuthUser(userId="test2", password="testtest")
@Nested
class NestTest {
@org.junit.jupiter.api.Test
public void testDemo2Tenant() throws IOException {
//do test as tenant:testDemo2 user:test2 ...
:
:
}
@NoAuthUser
@org.junit.jupiter.api.Test
public void testDemo2TenantNoAuth() throws IOException {
//do test as tenant:testDemo2 no auth ...
:
:
}
}
:
:
}
テストクラスは、Java、もしくはGroovyで記述可能です。 Groovyで記述された、Command、UtilityClassをテストする場合は、Groovyでテストクラスを記述した方が記述が簡易になります。 詳しくはそれぞれのテストクラスのサンプル実装を参照下さい。
Extension はテストファイル単位で作成される。本クラスでは、設定をテストファイル単位でスタックに格納し、テスト実行時に利用する。
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor
org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<T> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context) voidinterceptTestMethod(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<Void> invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext<Method> invocationContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor
interceptAfterAllMethod, interceptAfterEachMethod, interceptBeforeAllMethod, interceptBeforeEachMethod, interceptDynamicTest, interceptDynamicTest, interceptTestClassConstructor, interceptTestFactoryMethod, interceptTestTemplateMethod
-
Constructor Details
-
MTPJUnitTestExtension
public MTPJUnitTestExtension()
-
-
Method Details
-
beforeAll
- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Throws:
Exception
-
afterAll
- Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback- Throws:
Exception
-
interceptTestMethod
public void interceptTestMethod(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<Void> invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext<Method> invocationContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Throwable - Specified by:
interceptTestMethodin interfaceorg.junit.jupiter.api.extension.InvocationInterceptor- Throws:
Throwable
-