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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Junit / Fongo:如何在单元测试中使用Fongo来检查NotNull相关的知识,希望对你有一定的参考价值。

我正在编写一个基本的单元测试,检查返回的查询(类型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?

答案

我创建了an example,这说明了这一点。

以上是关于Junit / Fongo:如何在单元测试中使用Fongo来检查NotNull的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Junit进行单元测试

Java应用XVIII在 Java 中使用JUnit5进行单元测试和自动化测试

如何使用JUnit+JaCoCo+EclEmma完成单元测试

通过JUnit进行Android单元测试

如何使用 SpringBoot2、JUnit5 和 Kotlin 将配置属性注入单元测试

Junit - 期望异常测试(Expected Test)