How to properly mock @Startup CDI bean?

I have Quarkus/Jakarta REST API. It contains a CDI bean which is annotated with @Startup. In short, it’s initializing the main service class for that REST API.

My code looks like this:

@Path("/proxy")
public class ProxyResource {
  @Inject
  ApiManager apiManager;

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response proxy() {
    String response = apiManager.fetchResponse();
    return Response.ok(response).build();
  }
}

@Startup
public class ApiManager {
  public ApiManager() {
    logger.info("Actual ApiManager constructor");
  }

  public String fetchResponse() {
    return "expected response";
  }
}

By using @Startup I’m sure that when POST /proxy endpoint is called then apiManager object is already created and, most importantly, its constructor was invoked before actual API request. In this constructor I’m performing some “warmup” operations such as registering tasks for java.util.concurrent.ScheduledExecutorService.

I’m creating simple integration tests for POST /proxy endpoint. I want to mock ApiManager so that I can control its behavior and that I can focus the test on testing actual responses from that API.

My test currently looks like this:

@QuarkusTest
class ProxyResourceTest {

    @InjectMock
    private ApiManager apiManagerMock;

    @Test
    void testProxyEndpointReturns200AndResponse() {
      String payload = "expected response";

      Mockito.when(apiManagerMock.fetchResponse()).thenReturn(payload);

      given().contentType(ContentType.JSON)
        .body(payload)
        .when().post("/proxy")
        .then()
        .statusCode(200)
        .body(is(payload));
    }
  }
}

So here is my actual problem: When I execute quarkus test I can see that the constructor of ApiManager is executed. Most likely Quarkus does not inject the mock, but it’s injecting the actual object. If I change ApiManager annotation from @Startup to @ApplicationScoped (only for test run purpose), then tests are running as expected, mock is being injected thus constructor is not being executed.

Question:
How can I mock a @Startup CDI bean in the test so I can fully control behavior of the mocked object (and set expectations for mock; and – in that case – constructor won’t be executed)?

Most likely Quarkus does not inject the mock, but it’s injecting the actual object.

@Startup is actually translated to an observer method of StartupEvent which is always fired when the app starts.

If I change ApiManager annotation from @Startup to @ApplicationScoped (only for test run purpose), then tests are running as expected, mock is being injected thus constructor is not being executed.

This way no StartupEvent observer is registered and thus ApiManager is not created in order to receive the event.

How can I mock a @Startup CDI bean in the test so I can fully control behavior of the mocked object (and set expectations for mock; and – in that case – constructor won’t be executed)?

Well, you can use quarkus.arc.test.disable-application-lifecycle-observers=true to disable StartupEvent/ShutdownEvent observers declared on bean classes during the tests.

But I’m not completely sure it would help in your use case…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật