2

drowpizard リソースから返されたビューが正しく組み立てられていることをテストしようとしています。具体的には、ユーザーを追加するビューには、特定のルールを満たさない場合にパスワード チェッカーがユーザーの作成を拒否できるようにするフィールドがあります。ビューは、webapp をデプロイして実行し、不適切なパスワードを指定するとうまく機能しますが、単体テストを実行しようとすると、ビューにメッセージ本文ライターがないことを示す Web アプリケーション例外がスローされます。

私の単体テストはかなり単純です:

@Test
public void testBadPassword(){
    ClientResponse response = null;
    try {
         response=client().resource("/users").post(ClientResponse.class, createUserParameters);
         Status status = response.getClientResponseStatus();
         assertEquals(Status.SEE_OTHER, status);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}

サーバー 500 エラーが返されますが、これは次の中に埋もれています。

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.seeker.stm.resources.views.AddUserView, and Java type class com.seeker.stm.resources.views.AddUserView, and MIME media type text/html was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448)

リクエストを処理し、ビューとレスポンスを生成するリソースは次のようになります。

@Path("/users")
public class UserResource {

    public Object addUser(@FormParam("username") String username,
        @FormParam("password") String password,
        @FormParam("email") String email){
    Set<String> passwordErrors = passwordChecker.checkValidity(password, username, email);
    if (!passwordErrors.isEmpty()) {            
        return new AddUserView(userDAO.getAllActive(), passwordErrors);
    }

    // ...
}

私は例外を理解できます (StringProvider や ByteArrayProvider のように、それが知っているライターは、応答やビューなどに対しては機能しません) が、どちらかの応答を返すことができるリソースを適切にテストする方法を知りたいです。コード、またはビュー。

4

2 に答える 2

4

に追加する必要がありaddProvider(ViewMessageBodyWriter.class);ますsetUpResources

ViewMessageBodyWriterによってプロバイダーとして追加されViewBundleます。これが、サービスで機能する理由です (おそらく がある場所bootstrap.addBundle(new ViewBundle());)。

于 2013-03-29T00:01:04.223 に答える
0

私は dropwizard 0.8.0-rc3 を使用しており、ビューは freemarker テンプレート ベースです。上記を試してみましたが、例外が発生し続けました。最終的に私のために機能したのは、呼び出しでした

.addProvider(new ViewMessageBodyWriter(new MetricRegistry(), Collections.singleton(new FreemarkerViewRenderer())))

私のResourceTestRule.builder()

于 2015-02-26T13:24:29.707 に答える