Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rbygrave opened this issue May 19, 2022 · 0 comments
Assignees
Milestone

Comments

@rbygrave
Copy link
Contributor

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");
  }

}
@rbygrave rbygrave self-assigned this May 19, 2022
@rbygrave rbygrave added this to the 2.2 milestone May 19, 2022
rbygrave added a commit that referenced this issue May 19, 2022
…tarts server, creates client (for injection)
rbygrave added a commit that referenced this issue May 19, 2022
…tarts server, creates client (for injection)

Examples using avaje-kate (a composite of jex + avaje-inject + avaje-http)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant