From bab3ee120959d7ecf7992894c9649c1474986454 Mon Sep 17 00:00:00 2001 From: Hari Krishnan Date: Wed, 31 Jul 2024 18:13:43 +0530 Subject: [PATCH] - Removing API tests as Specmatic can handle that also - Updating README accordingly --- README.md | 4 +- build.gradle | 3 - .../com/component/orders/api/APITests.kt | 63 -------------- .../com/component/orders/api/apiTests.feature | 82 ------------------- 4 files changed, 2 insertions(+), 150 deletions(-) delete mode 100644 src/test/kotlin/com/component/orders/api/APITests.kt delete mode 100644 src/test/kotlin/com/component/orders/api/apiTests.feature diff --git a/README.md b/README.md index 9f22557..bb62277 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,10 @@ A typical web application might look like this. We can use Specmatic to practice 1. Spring boot 2. Specmatic 3. Specmatic Beta extension (for mocking Kafka) -4. Karate ## Run Tests -This will start the specmatic stub server for domain api and kafka mock using the information in specmatic.yaml and run contract tests using Specmatic and API Tests using karate. + +This will start the specmatic stub server for domain api and kafka mock using the information in specmatic.yaml and run contract tests using Specmatic. ```shell ./gradlew test ``` diff --git a/build.gradle b/build.gradle index ddc4f6e..aa98106 100644 --- a/build.gradle +++ b/build.gradle @@ -36,9 +36,6 @@ dependencies { implementation 'javax.validation:validation-api:2.0.1.Final' testImplementation 'org.springframework.boot:spring-boot-starter-test:3.2.0' - testImplementation('com.intuit.karate:karate-junit5:1.4.1') { - exclude group: 'io.netty' - } testImplementation('io.specmatic:junit5-support:2.0.3') testImplementation('io.specmatic:specmatic-kafka:0.22.5-TRIAL') testImplementation( diff --git a/src/test/kotlin/com/component/orders/api/APITests.kt b/src/test/kotlin/com/component/orders/api/APITests.kt deleted file mode 100644 index 52be9b2..0000000 --- a/src/test/kotlin/com/component/orders/api/APITests.kt +++ /dev/null @@ -1,63 +0,0 @@ -package com.component.orders.api - -import com.component.orders.Application -import com.intuit.karate.junit5.Karate -import io.specmatic.kafka.mock.KafkaMock -import io.specmatic.kafka.mock.model.Expectation -import io.specmatic.stub.ContractStub -import io.specmatic.stub.createStub -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.AfterAll -import org.junit.jupiter.api.BeforeAll -import org.springframework.boot.SpringApplication -import org.springframework.context.ConfigurableApplicationContext - -class APITests { - - @Karate.Test - fun apiTests(): Karate { - return Karate().path(KARATE_FEATURE_FILE).relativeTo(this::class.java) - } - - companion object { - private lateinit var context: ConfigurableApplicationContext - private lateinit var httpStub: ContractStub - private lateinit var kafkaMock: KafkaMock - private const val HTTP_STUB_HOST = "localhost" - private const val HTTP_STUB_PORT = 8090 - private const val KAFKA_MOCK_HOST = "localhost" - private const val KAFKA_MOCK_PORT = 9092 - private const val EXPECTED_NUMBER_OF_MESSAGES = 3 - - @BeforeAll - @JvmStatic - fun setUp() { - // Start Specmatic Http Stub - httpStub = createStub(HTTP_STUB_HOST, HTTP_STUB_PORT, strict = true) - - // Start Specmatic Kafka Mock and set the expectations - kafkaMock = KafkaMock.startInMemoryBroker(KAFKA_MOCK_HOST, KAFKA_MOCK_PORT) - kafkaMock.setExpectations(listOf(Expectation("product-queries", EXPECTED_NUMBER_OF_MESSAGES))) - - // Start Springboot application - context = SpringApplication.run(Application::class.java) - } - - @AfterAll - @JvmStatic - fun tearDown() { - // Shutdown Springboot application - context.close() - - // Shutdown Specmatic Http Stub - httpStub.close() - - val result = kafkaMock.stop() - assertThat(result.success).withFailMessage(result.errors.joinToString()).isTrue - // Wait for Kafka server to stop - Thread.sleep(5000) - } - - private const val KARATE_FEATURE_FILE = "apiTests.feature" - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/component/orders/api/apiTests.feature b/src/test/kotlin/com/component/orders/api/apiTests.feature deleted file mode 100644 index 9a10de6..0000000 --- a/src/test/kotlin/com/component/orders/api/apiTests.feature +++ /dev/null @@ -1,82 +0,0 @@ -Feature: Tests - - Scenario: Search for available products - # Arrange - setup expectations with Specmatic Http Stub - * def expectationJson = karate.read('classpath:stub_products_200.json') - Given url 'http://localhost:8090/_specmatic/expectations' - And request expectationJson - When method post - Then status 200 - - # Act - make the actual call to the service - Given url 'http://localhost:8080/findAvailableProducts?type=gadget' - And header pageSize = 10 - When method get - - # Assert - status and response body - Then status 200 - And match response == expectationJson["http-response"].body - - Scenario Outline: Search for available products - Error condition - # Arrange - simulate error by setting expectation with empty response body - * def expectationJsonForErrorCondition = karate.read('classpath:stub timeout.json') - Given url 'http://localhost:8090/_specmatic/expectations' - And request expectationJsonForErrorCondition - When method post - Then status 200 - - # Act - make the actual call to the service - Given url 'http://localhost:8080/findAvailableProducts?type=' + - And header pageSize = 10 - When method get - - # Assert - Then status 503 - - Examples: - | productType | - | "other" | - - Scenario Outline: Create order - # Arrange - setup expectations with Specmatic Http Stub - Given url 'http://localhost:8090/_specmatic/expectations' - And request - """ - { - "http-request": { - "method": "POST", - "path": "/orders", - "headers": { - "Authenticate": "API-TOKEN-SPEC" - }, - "body": { - "productid": 10, - "count": 1, - "status": "pending" - } - }, - - "http-response": { - "status": 200, - "body": { - "id": 10 - }, - "status-text": "OK", - } - } - """ - When method post - Then status 200 - - # Act - Given url 'http://localhost:8080/orders' - And request {"productid": , "count": } - When method post - - # Assert - Then status 201 - And assert response["id"] == - - Examples: - | productId | count | - | 10 | 1 | \ No newline at end of file