lordofthejars / nosql-unit

NoSQL Unit is a JUnit extension that helps you write NoSQL unit tests.
Other
383 stars 123 forks source link

com.lordofthejars.nosqlunit.core.NoSqlAssertionError: while testing save operation using fongo. #126

Open Abhishekgith opened 9 years ago

Abhishekgith commented 9 years ago

I have created a web application using spring mongodb, maven. I trying to test mongo db save operation but facing issues with nosqllordofthejars. Please find below the details:


I am getting below error: below is the stack trace:

com.lordofthejars.nosqlunit.core.NoSqlAssertionError: Object # { "_id" : "54f8242ce281d80fcaebc723" , "_class" : "com.docworks.domain.CRDetails" , "cr" : "cr2" , "desc" : "cr2 desc" , "packageNumber" : 34 , "certGuidelines" : { "certG" : "

certg

" , "userType" : "Certification Team" , "status" : "Pending with Document Developer" , "count" : 0} , "dueDate" : "Fri Oct 02 00:00:00 IST 2015" , "solutionName" : "Patient Accounting" , "certGuidelinesList" : " "} # is not found into collection [certguidelines] at com.lordofthejars.nosqlunit.core.FailureHandler.createFailure(FailureHandler.java:7) at com.lordofthejars.nosqlunit.mongodb.MongoDbAssertion.checkCollectionObjects(MongoDbAssertion.java:102) at com.lordofthejars.nosqlunit.mongodb.MongoDbAssertion.strictAssertEquals(MongoDbAssertion.java:36) at com.lordofthejars.nosqlunit.mongodb.DefaultComparisonStrategy.compare(DefaultComparisonStrategy.java:17) at com.lordofthejars.nosqlunit.mongodb.DefaultComparisonStrategy.compare(DefaultComparisonStrategy.java:1) at com.lordofthejars.nosqlunit.core.AbstractCustomizableDatabaseOperation.executeComparison(AbstractCustomizableDatabaseOperation.java:15) at com.lordofthejars.nosqlunit.mongodb.MongoOperation.compareData(MongoOperation.java:103) at com.lordofthejars.nosqlunit.mongodb.MongoOperation.databaseIs(MongoOperation.java:96) at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.assertExpectation(AbstractNoSqlTestRule.java:229) at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.evaluate(AbstractNoSqlTestRule.java:78) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


Below is the json data. I am populating for my test class. { "certguidelines":[ { "_id" : "54f8242ce281d80fcaebc723", "_class" : "com.docworks.domain.CRDetails", "cr" : "cr2", "desc" : "cr2 desc", "packageNumber" : 34, "certGuidelines" : { "certG" : "

certg

", "userType" : "Certification Team", "status" : "Pending with Document Developer", "count" : 0 }, "dueDate" : "Fri Oct 02 00:00:00 IST 2015", "solutionName" : "Patient Accounting", "certGuidelinesList" : [] } ]

}

Below is test class that I am trying to run:

package com.docworks.service.test; import static com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder.newMongoDbRule; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.docworks.domain.CRDetails; import com.docworks.domain.CertificateGuidelines; import com.docworks.exception.CRAlreadyExistsException; import com.docworks.service.CRService; import com.github.fakemongo.Fongo; import com.lordofthejars.nosqlunit.annotation.ShouldMatchDataSet; import com.lordofthejars.nosqlunit.mongodb.MongoDbRule; import com.mongodb.Mongo;

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:test-context.xml"}) public class CRServiceTest {

@Rule
public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb("test");

@Autowired 
private ApplicationContext applicationContext;

@Autowired 
@Qualifier("certGService")
private CRService certRepo;

private CRDetails createCertGuide() throws ParseException{
    final CRDetails certguide = new CRDetails();
final CertificateGuidelines certG = new CertificateGuidelines();
    final List<CertificateGuidelines> certGList = new ArrayList<CertificateGuidelines>();

    String text = "10/02/2015";
    SimpleDateFormat inFormat = new SimpleDateFormat("MM/dd/yyyy");
    Date date = inFormat.parse(text);
    certguide.setId("54f8242ce281d80fcaebc723");
    certguide.setCr("cr2");
    certguide.setDesc("cr2 desc");
    certguide.setPackageNumber(34);
    certG.setCertG("<p>certg</p>");
    certG.setUserType("Certification Team");
    certG.setStatus("Pending with Document Developer");
    certG.setCount(0);
    certguide.setCertGuidelines(certG);
    certguide.setDueDate(date);
    certguide.setSolutionName("Patient Accounting");
    certguide.setCertGuidelinesList(certGList);

    return certguide;
}

/* * Expected results are in "one-person.json" file \ @throws CRAlreadyExistsException

package com.docworks.domain; import static com.docworks.util.Constants.COLLECTION_NAME; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.format.annotation.DateTimeFormat;

@Document(collection = COLLECTION_NAME) public class CRDetails {

@Id
private String id;
private String cr;
private String desc;
private long packageNumber;
private CertificateGuidelines certGuidelines;
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date dueDate;
private String solutionName;
private List<CertificateGuidelines> certGuidelinesList = new ArrayList<CertificateGuidelines>();

public CRDetails()
{

}

public CRDetails(String cr, String desc, long packageNumber, CertificateGuidelines certGuidelines,String solutionName, Date dueDate) {
    super();
    this.cr = cr;
    this.desc = desc;
    this.packageNumber = packageNumber;
    this.certGuidelines = certGuidelines;
    this.dueDate = dueDate;
    this.solutionName = solutionName;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getCr() {
    return cr;
}

public void setCr(String cr) {
    this.cr = cr;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public long getPackageNumber() {
    return packageNumber;
}

public void setPackageNumber(long packageNumber) {
    this.packageNumber = packageNumber;
}
public Date getDueDate() {
    return dueDate;
}

public void setDueDate(Date dueDate) {
    this.dueDate = dueDate;
}

public CertificateGuidelines getCertGuidelines() {
    return certGuidelines;
}

public void setCertGuidelines(CertificateGuidelines certGuidelines) {
    this.certGuidelines = certGuidelines;
}

public String getSolutionName() {
    return solutionName;
}

public void setSolutionName(String solutionName) {
    this.solutionName = solutionName;
}

public List<CertificateGuidelines> getCertGuidelinesList() {
    return certGuidelinesList;
}

public void setCertGuidelinesList(List<CertificateGuidelines> certGuidelinesList) {
    this.certGuidelinesList = certGuidelinesList;
}

@Override
public String toString() {
    return "CrDetails [cr=" + cr + ", description=" + desc + "]";
}

}


This is my save implementation in the service class :

public void saveCertification(CRDetails certGuide, String userTypeSubmitted) throws CRAlreadyExistsException {

    if (certGuide == null) {
        throw new NullPointerException("CertGuide cannot be null");
    }

    // Check collection exists else create it.
    if (!certMongoTemplate.collectionExists(CRDetails.class)) {
        certMongoTemplate.createCollection(CRDetails.class);
    }
    /*
     * Check if CR already exists, if exists throw error. else set the
     * status and insert new certGuide
     */
    if (checkCRExists(certGuide.getCr())) {
        throw new CRAlreadyExistsException(
                "This CR already exists!! Please insert new CR.");
    } else {
        certGuide.getCertGuidelines().setStatus(
                getStatusForCertGuidelines(userTypeSubmitted));

        certMongoTemplate.insert(certGuide, Constants.COLLECTION_NAME);
    }
}
lordofthejars commented 9 years ago

Hi, first of all thank you so much for using nosqlunit. Is it possible to push this example in github so it would be easy to debug and's happen? If cannot then I can help you to debug nosqlunit to see what's happening. Keep in mind that nosqlunit relies findone query with all the expected document to mongodb, so if it is not found it is because the findone query returned no results.

robinelvin commented 9 years ago

I'm getting exactly the same error. Debugging the code I've found that the findOne method does not return anything but it is looking for expectedDataObject which is an instance of DBObject.

If I try a findOne with the object id it works. If I try to add a definition to my repository interface it will not autowire any more: "nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findOne found for type User!"

What should we be using to make this work?

Thanks

lordofthejars commented 9 years ago

have you inspected the mongodb database connecting with CLI if the same object that is passed in findOne is in reality inside db?

robinelvin commented 9 years ago

Connecting to a real Mongo does indeed insert the user. I have a single test which inserts 1 user and I am using Fongo. In your checkCollectionObjects method the count() check passes it is just the findOne which is failing. If I manually retrieve by id and print out properties within the test it matches the JSON values.

life1style1 commented 8 years ago

Any update on this.I am also getting the same error while trying to save.