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 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 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)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 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 interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException