我有以下型号:蒙戈生产和测试数据库之间切换 - Java的春季启动
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);
}
}
而下面的库:
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);
}
的development
数据库包含customers
集合,其中包含了真实数据 - 关于真实客户的信息。 作为TDD
的忠实粉丝,我创建了下面的单元测试:
@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);
}
}
}
当然,我有一个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);
}
}
在执行单元测试production
和test
数据库之间怎么能我转?
注意:您的数据模型对这个问题有什么作用,其他方面都与MongoDB有关?除此之外,您可以为此使用环境变量。 –