Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test with easymock Capture

I have the following code

Record rd = registerNewRecord();
<do some processing>
rd.setFinished(true);
updateRecord(rd);

The registerNewRecord method calls the RecordDao insert method, and updateRecord calls the update method on the same DAO.

I have the following easymock code:

Capture<Record> insertRc = new Capture<Record>();
RecordDao.insert(capture(insertRc));
Capture<Record> updateRc= new Capture<Record>();
RecordDao.update(capture(updateRc));

Problem is since above rd the same instance of Record that was inserted is being updated, the insertRc Capture object is being updated too. So i cannot assert that the finished flag is set to false at insert time.

What am i doing wrong?

like image 775
Nuno Furtado Avatar asked Oct 20 '25 15:10

Nuno Furtado


2 Answers

If the references within insertRC and updateRC both refer to the same object rd and this is changed during the update method, you will always see that the Record object has finished. However, you could test the first Captured object before the update call is made.

Capture<Record> insertRc = new Capture<Record>();
RecordDao.insert(capture(insertRc));
Record insertedRecord = insertRC.getValue();
org.junit.Assert.assertFalse(insertedRecord.isFinished());

Capture<Record> updateRc= new Capture<Record>();
RecordDao.update(capture(updateRc));
Record updatedRecord = updateRC.getValue();
org.junit.Assert.assertTrue(updatedRecord.isFinished());
like image 92
DoctorRuss Avatar answered Oct 23 '25 04:10

DoctorRuss


One idea is to clone the Record object when you capture it.

Implement a clone() method in your Record class, and then implement a custom Capture as follows:

public class RecordCloneCapture extends Capture<Record> {
    @Override
    public void setValue(Record value) {
        super.setValue(value == null ? null : value.clone());
    }
}

And change your test code to use it:

Capture<Record> insertRc = new RecordCloneCapture();
RecordDao.insert(capture(insertRc));
Capture<Record> updateRc= new RecordCloneCapture();
RecordDao.update(capture(updateRc));

If you can't implement clone() for some reason, your custom Capture class could just extract the information it needs (i.e. the Record finished flag) in the setValue method and store it.

like image 41
Joe Daley Avatar answered Oct 23 '25 03:10

Joe Daley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!