@Test public void testUsingReadPreference() throws Exception { this.template.execute( "readPref", new CollectionCallback<Object>() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { assertThat(collection.getOptions(), is(0)); assertThat(collection.getDB().getOptions(), is(0)); return null; } }); MongoTemplate slaveTemplate = new MongoTemplate(factory); slaveTemplate.setReadPreference(ReadPreference.SECONDARY); slaveTemplate.execute( "readPref", new CollectionCallback<Object>() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { assertThat(collection.getReadPreference(), is(ReadPreference.SECONDARY)); assertThat(collection.getDB().getOptions(), is(0)); return null; } }); }
private void checkPersonPersisted(MongoTemplate template) { template.execute( Person.class, new CollectionCallback<Object>() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { DBObject dbo = collection.findOne(); assertThat((String) dbo.get("name"), is("Oliver")); return null; } }); }