6

Dropwizard: 0.6.1 (jersey 1.15) を使用して resourceTest を実行すると、「依存関係がありません」という例外が発生しました。

私のテストファイル:

public class MyResourceImplTest extends ResourceTest {
   ........
    @Override
    protected void setUpResources() throws Exception {
        addResource(new MyResourceImpl(new myConfiguration()));
    }
}

例外:

Dec 13, 2012 2:10:41 PM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer <init>
INFO: Creating low level InMemory test container configured at the base URI http://localhost:9998/
Dec 13, 2012 2:10:42 PM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer start
INFO: Starting low level InMemory test container
Dec 13, 2012 2:10:42 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.15 10/30/2012 02:40 PM'
Dec 13, 2012 2:10:42 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for method public javax.ws.rs.core.StreamingOutput com.****************.********(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String) at parameter at index 0
Dec 13, 2012 2:10:42 PM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer stop
INFO: Stopping low level InMemory test container
4

3 に答える 3

2

ジャージーは注入できないようですHttpServletRequest

エンドポイントの 1 つがこのように構成されていますか?

public StreamingOutput something(@Context HttpServletRequest request, String a, String b) {}

もしそうなら、あなたのデザインを再考し、代わりに選ぶことをお勧めします。

@Context
private HttpContext context;

public StreamingOutput something(String a, String b) {

  System.out.println("Request info "+context.getRequest().getAbsolutePath());

}

これにより、よりクリーンなアプローチが得られる可能性があります。リソースの登録に依存している限り、Classリクエストごとに新しいインスタンスが保証され、スレッド化の問題を回避できます。

于 2012-12-18T16:09:32.153 に答える
2

私の問題は、InMemory コンテナーでサポートされている HttpServletRequest を挿入したことです。この場合、jetty grizzlyWebTestContainer または jetty をテスト コンテナーとして使用する必要があります。jersey-test-framework-grizzly に導入すると、dropwizard 自体に対して多くの依存関係の競合が発生したため、どちらもうまくいきませんでした。将来、dropwizard をアップグレードすると、これが再び発生する可能性があるため、すべての競合を解決しようとする価値はないと考えています。

一日の終わりに、Webサービスをテストするために、いくつかの統合テスト(Pythonで)を使用してjenkinsの仕事をすることになりました。例えば。デプロイ後、いくつかの http リクエストを送信し、レスポンス コードとレスポンスの内容を確認します。それははるかに簡単です。

于 2013-04-01T09:24:00.970 に答える
0

これを回避する方法は、コンテキストを挿入せずに抽象基本クラス リソースを定義し、実際のサービス用に小さな派生クラスを実装することでした。

@Path("/contextMethod")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyResourceWithContext extends BaseResource {
    @Context
    private HttpServletRequest request;

    protected String getUserID()
    {
        return request.getRemoteUser();
    }
}

テストを実行するときは、HttpServletRequest を使用しないテスト専用の代替派生クラスを実装します。ここでのボーナスの利点は、派生したテスト可能なクラスが (たとえば) コンテキストにハードコードされた値を挿入して、適切なテスト シナリオを作成できることです。

于 2015-08-19T14:34:42.417 に答える