JUnit / Spring / MongoDB:单元测试由于空值而失败

时间:2018-11-05 05:19:35

标签: spring mongodb unit-testing junit

我对单元测试还很陌生,因此开始编写一个简单的测试以确保使用assertJ返回的查询不为null。我正在使用Fongo进行单元测试,尽管没有错误,但返回的值始终为null。

这是要测试的课程:

@Repository
public class DataVersionDaoMongo extends MongoBaseDao<DataVersion> implements DataVersionDao {

    @Autowired
    MongoOperations mongoOperations;

    public DataVersionDaoMongo() {
        initType();
    }

    @Override
    public DataVersion findByDBAndCollection(String dbName, String collectionName) {
        //return mongoOperations.findOne(Query.query(Criteria.where("dbName").is(dbName).and("collectionName").is(collectionName)), DataVersion.class);
        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;
    //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"));  
        assertThat(dataVersionDaoMongo.findByDBAndCollection(dbname, collectionName)).isNotNull();
    }
}

我确定 dataVersionDaoMongo.findByDBAndCollection(dbname, collectionName)返回null(返回DataVersion对象为null),因此测试失败。我实际上将如何处理并使其返回不为null的DataVersion?

这是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;
    }
}

任何帮助将不胜感激!

P.S。

这是我在单元测试课中添加的内容:

@Autowired
    private MongoOperations mongoOperations;

然后

DataVersion dataVersion = new DataVersion();
dataVersion.setDbName("DBDataVersion");
dataVersion.setVersion("version1");
dataVersion.setCollectionName("DataVersion");
mongoOperations.insert(dataVersion);
assertThat(dataVersionDaoMongo.findByDBAndCollection(dataVersionDaoMongo.getDbName(), dataVersion.getCollectionName())).isNotNull();

单元测试通过了,因为它不再返回null,但是我不再使用Fongo。我不确定我在做什么是否正确。

1 个答案:

答案 0 :(得分:0)

您将文档插入测试中的 mycollection 集合中,但dao查询 DataVersion 集合。

此外,您无需在存储的对象中定义 dbName collectionName ,因此,针对这两个字段的查询不会选择它。