Personalmente preferisco utilizzare un database incorporato per i test. Puoi utilizzare FakeMongo
package com.backend.configuration;
import com.github.fakemongo.Fongo;
import com.mongodb.Mongo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@Configuration
@EnableMongoRepositories(basePackages = "com.backend.repository")
public class MongoConfiguration extends AbstractMongoConfiguration {
private static final String DB_NAME = "test";
@Override
protected String getDatabaseName() {
return DB_NAME;
}
@Override
@Bean
public Mongo mongo() {
return new Fongo(getDatabaseName()).getMongo();
}
@Override
protected String getMappingBasePackage() {
return "com.backend.domain";
}
}
Nella classe di prova:
package com.backend.repository;
import com.backend.configuration.MongoConfiguration;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfiguration.class)
public class CustomerRepositoryTest {
}
Per gli altri profili come sviluppo e produzione, puoi utilizzare il profilo di avvio primaverile (vedi doc
) oppure puoi usare il profilo Maven :in application.properties
aggiungi questa proprietà [email protected]@
quindi definiscilo in pom.xml
accroding al profilo utilizzato :
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<mongo.host>localhost</mongo.host>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<mongo.host>mongo</mongo.host>
</properties>
</profile>