Skip to content

New annotation to generate a test http client interface for a Controller #584

Closed
@rbygrave

Description

@rbygrave

Say we have a controller like below, and we want to test that controller:

@Controller
@Path("/ping")
class PingController {

    @Produces("text/plain")
    @Get
    String ping() {
          return Instant.now().toString();
    }
}

In a test, we specify that we desire a http client interface generated for this controller like say:

@GenerateHttpClient(controller=PingController.class, clientPattern="{}ClientApi")

... and that that generates a http client interface in test generated source like:

@Client
@Path("/ping")
interface PingClientApi {

    @Get
    HttpResponse<String> ping();
}

Note that the response types on the client interface should probably use HttpResponse so that the tests can access the response statusCode and response headers. So return HttpResponse<String> rather than String etc.

We can now inject the PingClientApi in a test like:

@InjectTest 
class MyPingControllerTest {

  @Inject PingClientApi pingClientApi;

    @Test
  void ping() {
        HttpResponse<String> res = pingClientApi.ping();

        assertThat(res.statusCode()).isEqualTo(200);
        assertThat(res.body()).isNotEmpty();
  }

}

What do we think about this idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions