Skip to content

Add support for component test with .. @Inject of Http client starts server, creates client (for injection) #38

@rbygrave

Description

@rbygrave

Example, @Inject HttpClientContext client

  • The server is started on a random port
  • The client is created using the port and injected into the test
  • At test completion the server is stopped
@InjectTest
class HelloControllerTest {

  @Inject HttpClientContext client;

  @Test
  void hello() {
    HttpResponse<String> hello = client.request().path("hello")
      .GET()
      .asString();

    assertThat(hello.statusCode()).isEqualTo(200);

  }
}

Example

  • Given HelloApi is a java interface annotated with @Client + @Get, @Post etc ...
  • A static field is used ... The client and server is run once and used for all the tests in the class
  • The server is started on a random port (for the class)
  • The client is created using the port and injected into the test (for the class, static field)
  • All tests run
  • At completion the server is stopped
import io.avaje.inject.test.InjectTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@InjectTest
class HelloController2Test {

  @Inject static HelloApi client;

  @Test
  void hello() {
    String hi = client.hi();
    assertThat(hi).isEqualTo("hisay+yo");
  }

  @Test
  void hello2() {
    String hi = client.hi();
    assertThat(hi).isEqualTo("hisay+yo");
  }

}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions