Junit / Fongo:如何在单元测试中使用Fongo来检查NotNull

时间:2018-11-06 05:54:14

标签: spring mongodb unit-testing junit fongo

我正在编写一个基本的单元测试,以检查返回的查询(DataVersion类型)是否不为空。我必须确保我正在使用Fongo来测试我的数据库。

这是回购类:

    @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类:

@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文件:

<?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?

1 个答案:

答案 0 :(得分:0)

我创建了Playground Link来说明这一点。