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