0

有人能帮助我吗。

我正在使用 java 11、Microprofile、JerseyTest、Mockito、Junit5 为控制器编写测试我在运行测试时遇到错误(org.glassfish.hk2.api.UnsatisfiedDependencyException),问题是他们没有注入@配置属性。

我有一个控制器

@Produces(MediaType.APPLICATION_JSON)
@Singleton
public class UserController {

    @ConfigProperty(name = "config.value", defaultValue = "test") 
    private String configProp;

    @Inject
    private userService service;

这是我的测试

@ExtendWith(MockitoExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class UserControllerTest extends JerseyTest {

    @Setter
    @Getter
    private HttpServletRequest sourceRequest;


    private UserService service = mock(UserService.class);


    private String configProp = "test";

    @BeforeAll
    public void beforeAll() throws Exception {
        super.setUp();
    }

    @AfterAll
    void afterAll() throws Exception {
        tearDown();
    }

    @BeforeEach
    void beforeEach() {
        setSourceRequest(null);
    }

    @Override
    protected void configureClient(ClientConfig config) {
        super.configureClient(config);
        config.register(new UserController());
    }


    @Override
    protected Application configure() {

        final ResourceConfig config = new ResourceConfig();
        config.register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(service).to(UserService.class);
                bind(configProp).to(String.class);
            }
        });


        config.packages(true, "com.example");
        config.register(org.glassfish.jersey.jsonb.JsonBindingFeature.class);
        forceSet(TestProperties.CONTAINER_PORT, "0");
        return config;
    }

    @Test
    void test_generateResult() {

        when(service
                .execute(Mockito.any(UserPayload.class)))
                        .thenReturn(
                                "result");

        Response response = target("/users").request()
                .post(Entity.xml(
                        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><user><value>test</value></user>"));

        Assertions.assertNotNull(response.getEntity());
        Assertions.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus(),
                "Http Response should be 200");
        Assertions.assertEquals(MediaType.APPLICATION_JSON,
                response.getHeaderString(HttpHeaders.CONTENT_TYPE),
                "Http Content-Type should be: " + MediaType.APPLICATION_JSON);

        verify(service, times(1)).execute(Mockito.any(
                UserPayload.class));
        verifyNoMoreInteractions(service);
    }


}

这是堆栈跟踪

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=String,parent=UserController,qualifiers={@org.eclipse.microprofile.config.inject.ConfigProperty(name="config.value", defaultValue="test")},position=0,optional=false,self=false,unqualified=null,1122733228)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:51)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:188)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:205)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:334)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
    at org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:59)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:759)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:722)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:692)
    at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.getInstance(AbstractHk2InjectionManager.java:160)
    at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.getInstance(ImmediateHk2InjectionManager.java:30)
    at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:105)
    at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:260)
    at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:51)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:86)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:69)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:38)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:173)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:247)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    at org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainer.handle(JdkHttpHandlerContainer.java:135)
    at jdk.httpserver/com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at jdk.httpserver/sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:82)
    at jdk.httpserver/com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
    at jdk.httpserver/sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:725)
    at jdk.httpserver/com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at jdk.httpserver/sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:694)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
4

0 回答 0