0

I am fairly new to unit test and started writing a simple test to make sure the returned query is not null using assertJ. I am using Fongo for my unit test and although I am having no error, the returned value is always null.

This is the class to be tested:

@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);

    }
}

This is my unit test:

@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();
    }
}

I am sure that dataVersionDaoMongo.findByDBAndCollection(dbname, collectionName) is returning null (It is returning DataVersion object which is null), so the test fails. How would I actually go about and make it return DataVersion that is not null?

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

Any help would be greatly appreciated!

P.S.

This is what I am adding in my unit test class:

@Autowired
    private MongoOperations mongoOperations;

Then

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

The unit test passes because it is no longer returning null, but then I am not making use of Fongo anymore. I am not sure if what I am doing is right or not.

lovprogramming
  • 593
  • 11
  • 24

1 Answers1

0

You insert the document into mycollection collection in the test but the dao queries DataVersion collection.

Also you don't define dbName and collectionName in the stored object, hence, it won't be picked by a query which targets that two fields.

denis.zhdanov
  • 3,734
  • 20
  • 25
  • Thanks for the help Denis! I understand where I am doing it wrong, but I am unsure how I can turn it into code (about how to insert document into DataVersion collection and how to define dbName and collectionName in the stored object). If you can guide me step by step, that would be great :) – lovprogramming Nov 05 '18 at 17:50
  • I am trying different things and I added my work in my post under P.S. Can you please check it? – lovprogramming Nov 05 '18 at 22:03
  • You can create a minimal but complete standalone project which illustrates the problem, I'll take a look – denis.zhdanov Nov 06 '18 at 03:38
  • I created a separate question with minimal info based on your comment. Thanks denis! Link: https://stackoverflow.com/questions/53166437/junit-fongo-how-to-make-use-of-fongo-in-the-unit-test-for-checking-notnull – lovprogramming Nov 06 '18 at 05:57
  • Replied there. Please put the sample project somewhere on github/bitbucket/etc next time to ease the setup. At least for myself - normally I wouldn't bother configuring the whole sample by myself :) – denis.zhdanov Nov 06 '18 at 13:23