Spring Data MongoDB でリポジトリ アプローチを使用していくつかのテストを構築するにはどうすればよいですか? この目的で本番データベースを使用したくないため、テスト用にテスト データベースを設定したいと考えています。おそらく可能なはずですが、私にはわかりません。これは私のアプリケーションコンテキストです:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
<mongo:options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="${mongo.connect-timeout}"
max-wait-time="${mongo.max-wait-time}"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="${mongo.socket-timeout}"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="${mongo.db}" />
</bean>
<context:component-scan base-package="domain.company.group.project.data.repositories"/>
<!-- MongoDB repositories -->
<mongo:repositories base-package="domain.company.group.project.data.repositories.mongodb"/>
<!-- some other stuff -->
</beans>
そして、次のような単純なリポジトリがあるとしましょう。
public interface LocationRepository extends MongoRepository<Location, String>, LocationRepositoryCustom {
}
ここで、LocationRepositoryImpl は、特定の Location (ドメイン オブジェクト) クラスのすべてのカスタム メソッドを実装するクラスです。私のテストクラスは次のようになります。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/test-context.xml"})
public class LocationRepositoryTest {
@Autowired
private LocationRepository locationRepository;
/* Some tests... */
}
実行中のテストに MongoDB インスタンスを埋め込もうとしましたが (ここで説明したように)、機能しません。テスト データベースへの接続は確立されていますが、すべての保存メソッドがデータを挿入し続けるため、mongo テンプレートを上書きできないようです。 「生産」データベース。
Spring 3.2.0 と Spring Data Mongo 1.1.0.RELEASE を使用しています。テストにはJunitを使用しています。
助言がありますか?
前もって感謝します。