问题描述
我正在编写一个基本的单元测试,以检查返回的查询(类型为DataVersion)是否不为空.我必须确保我正在使用Fongo来测试我的数据库.
I am writing a basic unit test that checks whether the returned query (type DataVersion) is not null. I have to make sure that I am using Fongo for testing my db.
这是回购类:
@Repository
public class DataVersionDaoMongo extends MongoBaseDao<DataVersion> implements DataVersionDao {
@Autowired
MongoOperations mongoOperations;
public DataVersionDaoMongo() {
initType();
}
@Override
public DataVersion findByDBAndCollection(String dbName, String collectionName) {
Criteria criteria = Criteria.where("dbName").is(dbName).and("collectionName").is(collectionName);
Query query = Query.query(criteria);
return mongoOperations.findOne(query, DataVersion.class);
}
}
这是我的单元测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/testApplicationContext.xml")
public class DataVersionDaoMongoTest {
@Autowired
private DataVersionDaoMongo dataVersionDaoMongo;
@Autowired
private MongoOperations mongoOperations;
private DataVersion dataVersion;
@Rule
public FongoRule fongoRule = new FongoRule();
@Test
public void findByDBAndCollection() {
//String dbname = "mydb";
//String collectionName = "mycollection";
// DB db = fongoRule.getDB(dbname);
//DBCollection collection = db.getCollection(collectionName);
//Mongo mongo = fongoRule.getMongo();
//collection.insert(new BasicDBObject("name", "randomName"));
DataVersion dataVersion = new DataVersion();
dataVersion.setDbName("DBDataVersion");
dataVersion.setVersion("version1");
dataVersion.setCollectionName("DataVersion");
mongoOperations.insert(dataVersion);**strong text**
assertThat(dataVersionDaoMongo.findByDBAndCollection(dbname, collectionName)).isNotNull();
}
}
这是DataVersion类:
Here is the DataVersion class:
@Document(collection = "DataVersion")
public class DataVersion {
@Id
private String id;
private String dbName;
private String collectionName;
private String version;
private boolean isCompleted;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean isCompleted) {
this.isCompleted = isCompleted;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((collectionName == null) ? 0 : collectionName.hashCode());
result = prime * result + ((dbName == null) ? 0 : dbName.hashCode());
result = prime * result + (isCompleted ? 1231 : 1237);
result = prime * result + ((version == null) ? 0 : version.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DataVersion other = (DataVersion) obj;
if (collectionName == null) {
if (other.collectionName != null)
return false;
} else if (!collectionName.equals(other.collectionName))
return false;
if (dbName == null) {
if (other.dbName != null)
return false;
} else if (!dbName.equals(other.dbName))
return false;
if (isCompleted != other.isCompleted)
return false;
if (version == null) {
if (other.version != null)
return false;
} else if (!version.equals(other.version))
return false;
return true;
}
}
这是testApplicationContext文件:
This is testApplicationContext file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<bean name="fongo" class="com.github.fakemongo.Fongo">
<constructor-arg value="InMemoryMongo" />
</bean>
<bean id="mongo" factory-bean="fongo" factory-method="getMongo" />
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" />
<!-- localhost settings for mongo -->
<!--<mongo:db-factory id="mongoDbFactory" />-->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory"/>
</bean>
</beans>
虽然我的单元测试通过了,因为我声明了dataVersion
变量并在测试中设置了诸如DbName,Version,CollectionName之类的值,并调用了方法findByDBandCollection
.但是我在这里没有使用Fongo DB,我想利用它.如何在测试中使用Fongo db并使用assertThat,以便通过调用findByDBAndCollection
方法返回的DataVersion不为null?
Although my unit test is passing because I declared dataVersion
variable and set the values such as DbName, Version, CollectionName inside my test and calls the method findByDBandCollection
. But I am not making use of Fongo DB here and I want to make use of it. How can I go about and make use of Fongo db in my test and use assertThat so that the DataVersion returned by calling findByDBAndCollection
method is not null?
推荐答案
我创建了一个示例对此进行了说明.
I created an example which illustrates that.
这篇关于Junit/Fongo:如何在单元测试中使用Fongo来检查NotNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!