Unit Test for Spring actuator endpoint extension - Stack Overflow

admin2025-04-15  4

I have a custom extension of the info endpoint where I would like to test the returned values:

@Component
@EndpointWebExtension(endpoint = InfoEndpoint.class)
public class InfoWebEndpointExtension {

  private final InfoEndpoint delegate;
  private final TenantProperties tenantProperties;

  public InfoWebEndpointExtension(InfoEndpoint delegate, TenantProperties tenantProperties) {
    this.delegate = delegate;
    this.tenantProperties = tenantProperties;
  }

  @ReadOperation
  public WebEndpointResponse<Map<String, Object>> info() {
    Map<String, Object> info = delegate.info();
    info.put("defaultLanguage", tenantProperties.getDefaultLanguage());
    info.put("supportedLanguages", tenantProperties.getSupportedLanguages());
    info.put("canton", tenantProperties.getCanton());
    return new WebEndpointResponse<>(info, 200);
  }
}

When I try to write a @WebMvcTest(InfoWebEndpointExtension.class) it fails to inject the delegate:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'infoWebEndpointExtension' defined in file [InfoWebEndpointExtension.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'org.springframework.boot.actuate.info.InfoEndpoint' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I tried to Autowire or Mock it in the test class, but it can't be found. Is there a way to tell the WebMvcTest to use this from the context? I also tried a few approaches with a full blown SpringBootTest but there I had similar issues.

转载请注明原文地址:http://anycun.com/QandA/1744724235a86743.html