Package com.exonum.binding.testkit
Class TestKitExtension
java.lang.Object
com.exonum.binding.testkit.TestKitExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.ParameterResolver
public class TestKitExtension
extends java.lang.Object
implements org.junit.jupiter.api.extension.ParameterResolver
Extension that injects TestKit into service tests and destroys afterwards. Register this
extension with TestKit builder and a TestKit will be injected as a parameter, instantiated from
given builder. Note that this extension shouldn't be static. Example usage:
@RegisterExtension
TestKitExtension testKitExtension = new TestKitExtension(
TestKit.builder()
.withDeployedArtifact(artifactId, artifactFilename)
.withService(artifactId, serviceName, serviceId)
.withArtifactsDirectory(artifactsDirectory));
@BeforeEach
void setUp(TestKit testKit) {
// Set up
}
@Test
void test(TestKit testKit) {
// Test logic
}
instead of:
private TestKit testKit;
@BeforeEach
void setUp() {
testKit = TestKit.withDeployedArtifact(artifactId, artifactFilename)
.withService(artifactId, serviceName, serviceId)
.withArtifactsDirectory(artifactsDirectory)
.build();
// Set up
}
@Test
void test() {
// Test logic
}
@AfterEach
void destroyTestKit() {
testKit.close();
}
As different tests might need slightly different TestKit configuration, following parameterization annotations are available:
Validator
sets main TestKit node type to validatorAuditor
sets main TestKit node type to auditorValidatorCount
sets number of validator nodes in the TestKit network
@RegisterExtension
TestKitExtension testKitExtension = new TestKitExtension(
TestKit.builder()
.withDeployedArtifact(artifactId, artifactFilename)
.withService(artifactId, serviceName, serviceId)
.withArtifactsDirectory(artifactsDirectory));
@Test
void test(@Auditor @ValidatorCount(8) TestKit testKit) {
// Test logic
}
Note that after TestKit is instantiated in given test context, it is not possible to reconfigure it again. For example, if TestKit is injected in @BeforeEach method, it can't be reconfigured in @Test or @AfterEach methods.
Also note that TestKit can't be injected in @BeforeAll and @AfterAll methods.
-
Constructor Summary
Constructors Constructor Description TestKitExtension(TestKit.Builder templateTestKitBuilder)
-
Method Summary
Modifier and Type Method Description java.lang.Object
resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
boolean
supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
-
Constructor Details
-
Method Details
-
supportsParameter
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)- Specified by:
supportsParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
-
resolveParameter
public java.lang.Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException- Specified by:
resolveParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-