Introducing Stove Testing Dsl
Example testing context

Introducing Stove Testing Dsl

Stove 0.3.0 is published ??

It brings nice additions that you can enjoy using.

  • A new Testing and Setup Dsl that gives joy while writing #tests with #kotlin.
  • A Bridging feature for the Application Under Test to change the entry points of the use case of the test

Here is how you can set up this #testing scenario on the image:

TestSystem(baseUrl = "https://localhost:8001") {
    if (isRunningLocally()) {
        enableReuseForTestContainers()
        
        // this will keep the dependencies running 
        // after the tests are finished
        // so next run will be blazing fast :)
        keepDendenciesRunning() 
    }
}.with {
    // Enables http client 
    // to make real http calls 
    // against the application under test
    http()

    // Enables bridging to application under test
    bridge()

    // Enables Couchbase physically 
    // and exposes the configuration 
    // to the application under test
    couchbase {
        CouchbaseSystemOptions(
            defaultBucket = "Stove",
            configureExposedConfiguration = { cfg -> listOf("couchbase.hosts=${cfg.hostsWithPort}") },
        )
    }

    // Enables Kafka physically 
    // and exposes the configuration 
    // to the application under test
    kafka {
        KafkaSystemOptions(
            configureExposedConfiguration = { cfg -> listOf("kafka.bootstrapServers=${cfg.boostrapServers}") },
        )
    }

    // Enables Wiremock on the given port 
    // and provides configurable mock HTTP server 
    // for your external API calls
    wiremock {
        WireMockSystemOptions(
            port = 9090,
            removeStubAfterRequestMatched = true,
            afterRequest = { e, _, _ ->
                logger.info(e.request.toString())
            },
        )
    }

   // Enables Elasticsearch    
   elasticsearch {
        ElasticsearchSystemOptions(
           DefaultIndex(index = "testIndex", migrator = TestIndexMigrator()),
           clientConfigurer = ElasticClientConfigurer(
               restClientOverrideFn = Some { cfg ->
                   RestClient.builder(HttpHost(cfg.host, cfg.port)).build()
               }
           )
        ).migrations { register<AdditionalIndexMigrator>() }
     } 

    // The Application Under Test. 
    // Enables Spring Boot application 
    // to be run with the given parameters.
    springBoot(
        runner = { parameters ->
            stove.spring.example.run(parameters) { it.addTestSystemDependencies() }
        },
        withParameters = listOf(
            "server.port=8001",
            "logging.level.root=warn",
            "logging.level.org.springframework.web=warn",
            "spring.profiles.active=default",
            "kafka.heartbeatInSeconds=2",
        ),
    )
}.run()        

And write #tests for it:

TestSystem.validate {
    // Arrange
    wiremock {
        mockGet("/example-url", responseBody = None, statusCode = 200)
    }

    //Act/Assert
    http {
        get<String>("/hello/index") { actual ->
            actual shouldContain "Hi from Stove framework"
            println(actual)
        }
    }
    
    // Element of application under test
    using<PersonService> {
         record(Person(name = "John", id = "123"))    
    }

    couchbase {
        shouldQuery<Any>("SELECT * FROM system:keyspaces") { actual ->
            println(actual)
        }
    }

    kafka {
        shouldBePublishedOnCondition<ExampleMessage> { actual ->
            actual.aggregateId == 123
        }
        shouldBeConsumedOnCondition<ExampleMessage> { actual ->
            actual.aggregateId == 123
        }
    }

    couchbase {
        save(collection = "Backlogs", id = "id-of-backlog", instance = Backlog("id-of-backlog"))
    }

    http {
        postAndExpectBodilessResponse("/backlog/reserve") { actual ->
            actual.status.shouldBe(200)
        }
    }

    kafka {
        shouldBeConsumedOnCondition<ProductCreated> { actual ->
            actual.aggregateId == expectedId
        }
    }
}        

For more information, please check the source code and examples!

Damian Hickey

IAM | Product Development | Architecture | Building Teams

1 年

Sound like you are having fun ;)

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了