add simple java config

This commit is contained in:
DOHA 2017-02-09 01:34:42 +02:00
parent 2c8e3c9179
commit 6e114b6fb0
2 changed files with 33 additions and 6 deletions

View File

@ -0,0 +1,25 @@
package org.baeldung.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
@Configuration
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
public class SimpleMongoConfig {
@Bean
public Mongo mongo() throws Exception {
return new MongoClient("localhost");
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongo(), "test");
}
}

View File

@ -1,8 +1,10 @@
package org.baeldung.mongotemplate;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.baeldung.config.MongoConfig;
import org.baeldung.config.SimpleMongoConfig;
import org.baeldung.model.User;
import org.junit.After;
import org.junit.Before;
@ -15,7 +17,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
@ContextConfiguration(classes = SimpleMongoConfig.class)
public class MongoTemplateProjectionLiveTest {
@Autowired
@ -38,7 +40,7 @@ public class MongoTemplateProjectionLiveTest {
mongoTemplate.insert(new User("John", 30));
mongoTemplate.insert(new User("Ringo", 35));
Query query = new Query();
final Query query = new Query();
query.fields()
.include("name");
@ -55,7 +57,7 @@ public class MongoTemplateProjectionLiveTest {
mongoTemplate.insert(new User("John", 30));
mongoTemplate.insert(new User("Ringo", 35));
Query query = new Query();
final Query query = new Query();
query.fields()
.exclude("_id");
@ -64,7 +66,7 @@ public class MongoTemplateProjectionLiveTest {
assertNull(user.getId());
assertNotNull(user.getAge());
});
}
}