我有以下型號:蒙戈生產和測試數據庫之間切換 - 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有關?除此之外,您可以爲此使用環境變量。 –