mideo / api-test-kit

Test library for quick bootstrapping API Acceptance/Integration and Performance tests base classes backed with JUnit, Wiremock, Atam4J, and Gatling.

GitHub

api-test-kit

Build Status

Modules

Maven Central api-test-kit

Maven Central api-test-kit-api

Maven Central api-test-kit-performance

Maven Central api-test-kit-core

Maven Central api-test-kit-monitoring

Test library for quick bootstrapping API Acceptance/Integration and Performance tests base classes backed with JUnit, Wiremock, Atam4J, and Gatling.

For more information on test tools see:

Java Utilities

  • RestAssuredSpecFactory - Create rest-assured request spec with all wapi required headers
public class Example {
        public static void main(String[] args) throws Exception {
            RestAssuredSpecFactory.givenARequestSpecificationWithAllRequiredHeaders()
                   .when()
                   .post("http://exmaple.com")
                   .then().statusCode(200);
       
    }
}
  • AtamApplication - Acceptance Test as Monitors bootstrap
public class ExampleAtamApplication extends Atam4jMainApplication {
        public static void main(String[] args) throws Exception {
        if (args == null || args.length == 0) {
            args = new String[]{"server", getAtam4JConfigFile()};
        }
        new ExampleAtamApplication().run(args);
    }
}

  • ApiTest - Bootstrap api tests by starting and shutting down the test application
class ExampleApiTest extends ApiTest {

    @Override
    void startApplication() {
        Spark.start();
    }

    @Override
    void stopApplication() {
        Spark.stop();
    }

    @Override
    void loadTestApplicationProperties() {
        try {
            properties.load(getClass().getClassLoader().getResourceAsStream("test.properties"));
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
  • WiremockBasedApiTest - Bootstrap api tests by starting and shutting down the test application and wiremock
class ExampleWiremockBasedApiTest extends WireMockBasedApiTest {

    @Override
    void startApplication() {
        Spark.start();
    }

    @Override
    void stopApplication() {
        Spark.stop();
    }

    @Override
    void loadTestApplicationProperties() {
        try {
            properties.load(getClass().getClassLoader().getResourceAsStream("test.properties"));
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
  • JsonParser - quickly serialize/deserialize json backed by jackson
    public class DummyExample {
           
        static class Dummy {
    
    
            public String getName() {
                return name;
            }
    
            private String name;
    
            public void setName(String name) {
                this.name = name;
            }
    
        }

        public static void main(String[] args) {
        
            Dummy d = JsonParser.deserialize("{\"name\":\"Dummy\"}", Dummy.class);
            String payload = JsonParser.serialize(ImmutableMap.of("abc", "123"));
        }
    }
  • StubBuilder - wiremock backed stubbing
public class Example {
         public static void main(String[] args) throws Exception {
             StubBuilder stubServer = new StubBuilder().startWireMock();
             
             //stubbing
             stubServer.givenWiremockWillReturnCode(200);
             
             //set wiremock as proxy and record conversation with a third party service for future use
             StubRecorder recorder = stubBuilder.recorder("https://example.com", 443).record(
                             () -> requestSpecification
                                     .when()
                                     .get("/blueRed")
                                     .then()
                                     .statusCode(404)
                     );
             
                     
             List<StubMapping> mappings = recorder.saveRecording().then().getRecording();
             
     }
 }

Scala Utilities

  • PerformanceTest - Base Performance Test Class
  • LocalPerformanceTest - Performance Test Class bootstrapping of start and shutdown local test application
  • MockedLocalPerformanceTest - Performance Test Class bootstrapping of start and shutdown local test application and Wiremock
  • PerformanceTestConfig - Performance Test Config
import com.github.mideo.apitestkit.AsciiArt
import io.gatling.core.Predef.{atOnceUsers, scenario}
import io.gatling.core.controller.inject.InjectionStep
import io.gatling.core.structure.{PopulationBuilder, ScenarioBuilder}
import io.gatling.http.Predef._
import io.gatling.core.Predef._
import org.junit.runner.RunWith




object FakeScenario extends TestScenario {
  override def scenarioBuilder(business: String, userName: String): ScenarioBuilder = {
    scenario(s"Fake Scenario $business")
      .exec(http("fake request").get("/"))
  }
}

class FakeLocalPerformanceTest
  extends LocalPerformanceTest{
  AsciiArt.draw("TestLibs")
  override val testScenarios: List[TestScenario] = List(FakeScenario)
  override val simulations: List[PopulationBuilder] = testScenarios map { _.scenarioBuilder("test", "simulation").inject(injection) }

  override def injection: InjectionStep = atOnceUsers(1)
  doSetUp()

  override def startApplication(): Unit = {}

  override def stopApplication(): Unit = {}
}

class FakeMockedPerformanceTest
  extends LocalPerformanceTest{
  AsciiArt.draw("TestLibs")
  override val testScenarios: List[TestScenario] = List(FakeScenario)
  override val simulations: List[PopulationBuilder] = testScenarios map { _.scenarioBuilder("test", "simulation").inject(injection) }
  
  ScalaStubBuilder.givenWiremockWillReturnCode(200)
  
  override def injection: InjectionStep = atOnceUsers(1)
  doSetUp()

  override def startApplication(): Unit = {}

  override def stopApplication(): Unit = {}
}

class FakePerformanceTest
  extends PerformanceTest{
  AsciiArt.draw("TestLibs")
  override val testScenarios: List[TestScenario] = List(FakeScenario)
  override val simulations: List[PopulationBuilder] = testScenarios map { _.scenarioBuilder("test", "simulation").inject(injection) }

  override def injection: InjectionStep = atOnceUsers(1)
  doSetUp()
}


object FakeRunner {
  def main(args:Array[String]): Unit ={
    
    PerformanceTestConfig.forUrl("performanceTest.url")
        .withSuccessfulRequestPercentage(99)
      .withRequestPerSecond(5)
    
    GatlingTestRunner.forSimulationClass(classOf[FakePerformanceTest].getName)
      .fromSourceDirectory("src/test")
      .saveResultDirectoryTo("build/reports/performanceTest")
      .run
    
  } 
}