0

I am trying to test my API in a Quarkus APP. My test setup is I define an object and persist it to the DB with a direct call to the service object. I then call the API and expect to get the object back, but I don't....

My test class setup is;

@QuarkusTest
@TestTransaction
@TestHTTPEndpoint(CompanyController.class)
public class CompanyControllerIntegrationTest {

    @Inject
    CompanyService service;

    @ClassRule
    private static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:13.3-alpine")
            .withDatabaseName("db")
            .withUsername("user")
            .withPassword("password");

    @Test
    void test_getCompanyThatExist() {
        service.createCompany(createCompany());

        given()
                .when().get("/1")
                .then().statusCode(200)
                .body("size()", is(1));
    }

    private Company createCompany() {
        Company company = new Company();
        return company;
    }
}

Controller endpoint is;

@GET
@Path("/{id}")
public Response getCompany(@PathParam("id") Long id) {
    System.out.println("[CompanyController] Getting company with id - " + id);
    Company company = service.getCompany(id);
    System.out.println("[CompanyController] Company got was " + company);
    return Response
            .ok()
            .entity(company)
            .build();

Service call is;

public Company getCompany(Long id) {
    Company company = repository.findById(id);
    System.out.println("[CompanyService] Got company - " + company);
    return company;
}

And the print outs, which really confuses me.... console.logs

So the object is persisted with an ID of 1, but when I go to get the object with the ID of 1 its null. Any ideas why? As I am completely stumped at this stage.

Dan
  • 2,020
  • 5
  • 32
  • 55

1 Answers1

1

QuarkusTest annotation uses JUnit Jupiter's ExtendWith annotation so in this case you should use @Container instead of @ClassRule, add @Testcontainer at class level and add org.testcontainers:junit-jupiter to your pom.xml or gradle.build

Your test should looks like

@Testcontainers
@QuarkusTest
@TestTransaction
@TestHTTPEndpoint(CompanyController.class)
public class CompanyControllerIntegrationTest {

    @Inject
    CompanyService service;

    @Container
    private static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:13.3-alpine")
            .withDatabaseName("db")
            .withUsername("user")
            .withPassword("password");
}

The container can be started/stopped manually too

@QuarkusTest
@TestTransaction
@TestHTTPEndpoint(CompanyController.class)
public class CompanyControllerIntegrationTest {

    @Inject
    CompanyService service;

    private static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:13.3-alpine")
            .withDatabaseName("db")
            .withUsername("user")
            .withPassword("password");

    @BeforeAll
    void beforeAll() {
        db.start();
    }

    @AfterAll
    void beforeAll() {
        db.stop();
    }
}
Eddú Meléndez
  • 6,107
  • 1
  • 26
  • 35
  • So I have done that, but still end up with the same situation. The entity is added to the db on setup but when I query for it, it returns null – Dan Jul 20 '22 at 17:52