UnitTest是一个SpringBatch FieldSetMapper

时间:2014-02-04 13:03:55

标签: java junit spring-batch

我得到了以下自定义FieldSetMapper

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import de.BasicProperty;

public class PersonItemFieldSetMapper implements FieldSetMapper<PersonItem> {

    @Override
    public PersonItem mapFieldSet(final FieldSet fieldSet) throws BindException {
        PersonItem person = new PersonItem();
        person.setLastName(fieldSet.readString(BasicProperty.BASICPROP_LASTNAME));
        person.setFirstName(fieldSet.readString(BasicProperty.BASICPROP_FIRSTNAME));
        person.setEmployeeType(fieldSet.readString("EmployeeType"));
        person.setFormOfAdress(fieldSet.readString(BasicProperty.BASICPROP_FORMOFADDRESS));
        person.setEMail(fieldSet.readString(BasicProperty.BASICPROP_EMAIL));
        person.setUpn(fieldSet.readString(BasicProperty.BASICPROP_UPN));
        person.setWorkforceId(fieldSet.readString(BasicProperty.BASICPROP_WORKFORCE_ID));
        person.setInstitute(fieldSet.readString(BasicProperty.BASICPROP_INSTITUTE));
        person.setPhoto(fieldSet.readString(BasicProperty.BASICPROP_PHOTO));

        return person;
    }

}

目前我正在尝试为此映射器编写单元测试。 第一个问题:这合理吗? 第二个问题:我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

是的,您可以为mapFieldSet()

编写测试用例
You can always write such test cases to make sure your mapping are good!
// Using EasyMock to mock FieldSet  
@Test
public void mapFieldSetTest()
{
    FieldSet mockFieldSet = EasyMock.createMock(FieldSet.class);

    EasyMock.expect(mockFieldSet.readString("LNAME")).andReturn("Doe");
    EasyMock.expect(mockFieldSet.readString("FNAME")).andReturn("John");
    EasyMock.expect(mockFieldSet.readString("ETYPE")).andReturn("PART-TIME");
    EasyMock.expect(mockFieldSet.readString("FADDRESS")).andReturn("191, Santa Clara");
    EasyMock.expect(mockFieldSet.readString("EMAIL")).andReturn("john.Doe@fb.com");
    EasyMock.expect(mockFieldSet.readString("UPN")).andReturn("1111111111");
    EasyMock.expect(mockFieldSet.readString("WFID")).andReturn("22222222");
    EasyMock.expect(mockFieldSet.readString("INS")).andReturn("CA University College");
    EasyMock.expect(mockFieldSet.readString("PIC")).andReturn("c:\\john_doe.jpg");

    EasyMock.replay(mockFieldSet);

    // call the method under test
    PersonItem actual = fieldSetMapper.mapFieldSet(mockFieldSet);

    EasyMock.verify(mockFieldSet);

    // assert actual data by getters..
}