add simple java config
This commit is contained in:
parent
2c8e3c9179
commit
6e114b6fb0
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
package org.baeldung.mongotemplate;
|
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.baeldung.model.User;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -15,7 +17,7 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = MongoConfig.class)
|
@ContextConfiguration(classes = SimpleMongoConfig.class)
|
||||||
public class MongoTemplateProjectionLiveTest {
|
public class MongoTemplateProjectionLiveTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -38,7 +40,7 @@ public class MongoTemplateProjectionLiveTest {
|
|||||||
mongoTemplate.insert(new User("John", 30));
|
mongoTemplate.insert(new User("John", 30));
|
||||||
mongoTemplate.insert(new User("Ringo", 35));
|
mongoTemplate.insert(new User("Ringo", 35));
|
||||||
|
|
||||||
Query query = new Query();
|
final Query query = new Query();
|
||||||
query.fields()
|
query.fields()
|
||||||
.include("name");
|
.include("name");
|
||||||
|
|
||||||
@ -55,7 +57,7 @@ public class MongoTemplateProjectionLiveTest {
|
|||||||
mongoTemplate.insert(new User("John", 30));
|
mongoTemplate.insert(new User("John", 30));
|
||||||
mongoTemplate.insert(new User("Ringo", 35));
|
mongoTemplate.insert(new User("Ringo", 35));
|
||||||
|
|
||||||
Query query = new Query();
|
final Query query = new Query();
|
||||||
query.fields()
|
query.fields()
|
||||||
.exclude("_id");
|
.exclude("_id");
|
||||||
|
|
||||||
@ -64,7 +66,7 @@ public class MongoTemplateProjectionLiveTest {
|
|||||||
assertNull(user.getId());
|
assertNull(user.getId());
|
||||||
assertNotNull(user.getAge());
|
assertNotNull(user.getAge());
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user