私は、次のモデルがあります:development
データベースが含まれているcustomers
のコレクションが含まれていスイッチ - Javaの春ブーツ
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
:
import org.springframework.data.annotation.Id;
@Document
public class Customer {
@Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
そして、次のリポジトリを実データ - 実際の顧客に関する情報。私はもちろん
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {CustomerTest.class})
public class CustomerTest {
@Autowired
private CustomerRepository repository;
@Before
public void setUp() throws Exception {
// I want to delete the test database, not the production one
repository.deleteAll();
}
@Test
public void testCreateCustomer() {
// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}
}
}
ユニットテストを実行しながら、私はproduction
とtest
データベース間で切り替えることができますどのようにSpringMongoConfig.java
@Configuration
@PropertySource(value={"classpath:application.properties"})
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Value("${spring.data.mongodb.host}")
private String host;
@Value("${spring.data.mongodb.port}")
private Integer port;
@Value("${spring.data.mongodb.database}")
private String database;
public @Bean
MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(this.host + ":" + this.port), database);
}
public @Bean
MongoTemplate mongoTemplate() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory()),
new MongoMappingContext());
converter.setMapKeyDotReplacement("\\+");
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(), converter);
return mongoTemplate;
}
@Override
protected String getDatabaseName() {
return this.database;
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient(this.host, this.port);
}
}
: はTDD
の大ファンとして、私は以下のユニットテストを作成しましたか?
注:MongoDBとは何か関係がありますか?これ以外にも、環境変数を使用することができます。 –