1

ジャージーテストフレームワークで作業するのに苦労しています。

ルート リソースがあります。

@Path("sample")
public class SampleResource {

    @GET
    @Path("path")
    @Produces({MediaType.TEXT_PLAIN})
    public String readPath() {
        return String.valueOf(path);
    }

    @Inject
    private java.nio.file.Path path;
}

を提供する工場を用意しましたpath

public class SamplePathFactory implements Factory<Path> {

    @Override
    public Path provide() {
        try {
            return Files.createTempDirectory(null);
        } catch (final IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    @Override
    public void dispose(final Path instance) {
        try {
            Files.delete(instance);
        } catch (final IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }
}

そしてバインダー。

public class SamplePathBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bindFactory(SamplePathFactory.class).to(Path.class);
    }
}

そして最後に、私のテストクラスです。

public class SampleResourceTest extends ContainerPerClassTest {

    @Override
    protected Application configure() {
        final ResourceConfig resourceConfig
            = new ResourceConfig(SampleResource.class);
        resourceConfig.register(SamplePathBinder.class);
        return resourceConfig;
    }
}

私がテストしようとしたとき、私は得ました。

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Path,parent=SampleResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1916953383)

私は何を間違えましたか?

4

1 に答える 1

1

は、クラスとしてではなく、インスタンスAbstractBinderとして登録する必要があります。だから変更を加える

resourceConfig.register(new SamplePathBinder());

そしてそれはうまくいくはずです

于 2015-04-21T07:07:05.250 に答える