2016-04-13 14 views
1

クエリをmongoコレクションに偽装し、正しいパラメータがクエリに設定されていることを確認します。mongoクエリを黙っ​​て

テストされているコードは

Bson query = eq("name",name); 
if (version!=null) { 
    query = and(query,eq("version",version)); 
} 
Document retrievedDoc = collection.find(query).sort(descending("version")).first(); 

であると私は書きたいテストは、しかし、BSONは等号を実装していないと、それはいずれかを取得するために、任意の明白な方法を持っていない

Bson query = and(eq("name","test.TestClass"),eq("version",1)); 
when(collection.find(<some matcher>).thenReturn(result); 

です捕獲されたBsonを使ってカスタムマッチャーを書くか、テストするための内容です。

私はそれが重要だとは思わないが、私はモキトを使っている。

クエリよりも何かをアサーションする方法はありますか?

+0

私は場合でも、クエリのテスト容易性を可能にする任意のソリューションに興味を持っていることを追加します重要なリファクタリング – TomForwood

答えて

0

私も同じ質問がありました。 Bsonはequalsを実装していないので、私はBsonの代わりにorg.bson.Documentを使用します。

ここで私はそれを行う方法:

Document query = new Document("name",name); 
if (version!=null) { 
    query = new Document("$and", asList(query, new Document("version",version))); 
} 
Document retrievedDoc = collection.find(query).sort(descending("version")).first(); 

とテストは次のようにする必要があります:

Document query = new Document("$and", asList(new Document("name","test.TestClass"), new Document("version",1))); 
when(collection.find(query).thenReturn(result); 
関連する問題