Feature Tests

If you are familiar with Gherkin or Cucumber or other similar testing languages and frameworks, then feature testing will feel somewhat familiar. In Finatra, a feature test always consists of an app or a server under test. See the FeatureTest trait.

We highly recommend writing feature tests for your services as they provide a very good signal of whether you have correctly implemented the features of your service. If you haven’t implemented the feature correctly, it almost doesn’t matter that you have lots of unit tests.

HTTP Server

For example, to write a feature test for an HTTP server, extend the c.t.inject.server.FeatureTest trait. Then override the server with an instance of your EmbeddedHttpServer.

import com.twitter.finatra.http.EmbeddedHttpServer
import com.twitter.inject.server.FeatureTest

class ExampleServerFeatureTest extends FeatureTest {
  override val server = new EmbeddedHttpServer(new ExampleServer)

  test("ExampleServer#perform feature") {
      server.httpGet(
        path = "/",
        andExpect = Status.Ok)
        ...
    }
  }
}

Thrift Server

Similarly, to write a feature test for a Thrift server and create a client to it, extend the c.t.inject.server.FeatureTest trait, override the server with an instance of your EmbeddedThriftServer, and create a thrift client from the EmbeddedThriftServer.

import com.twitter.finatra.thrift.EmbeddedThriftServer
import com.twitter.inject.server.FeatureTest

class ExampleThriftServerFeatureTest extends FeatureTest {
  override val server = new EmbeddedThriftServer(new ExampleThriftServer)

  lazy val client = server.thriftClient[ExampleThrift[Future]](clientId = "client123")

  test("ExampleThriftServer#return data accordingly") {
      Await.result(client.doExample("input")) should equal("output")
    }
  }
}

Combined HTTP & Thrift Server

If you are extending both c.t.finatra.http.HttpServer and c.t.finatra.thrift.ThriftServer then you can feature test by constructing an EmbeddedHttpServer with ThriftClient, e.g.,

import com.twitter.finatra.http.EmbeddedHttpServer
import com.twitter.inject.server.FeatureTest

class ExampleCombinedServerFeatureTest extends FeatureTest {
  override val server =
    new EmbeddedHttpServer(new ExampleCombinedServer) with ThriftClient

  lazy val client = server.thriftClient[ExampleThrift[Future]](clientId = "client123")

  "ExampleCombinedServer#perform feature") {
      server.httpGet(
        path = "/",
        andExpect = Status.Ok)
        ...
    }

   "ExampleCombinedServer#return data accordingly") {
      Await.result(client.doExample("input")) should equal("output")
    }
  }
}

Caution

The server is specified as a def in the c.t.inject.server.FeatureTestMixin trait. If you only want to start one instance of your server per test file make sure to override this def with a val.

For more advanced examples see: