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 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 validator
  • Auditor sets main TestKit node type to auditor
  • ValidatorCount sets number of validator nodes in the TestKit network
These annotations should be applied on TestKit parameter:

 @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 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 interface org.junit.jupiter.api.extension.ParameterResolver
    • resolveParameter

      public 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 interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException