JUnit测试从一类到另一类的转换

时间:2019-01-14 16:08:52

标签: java testing intellij-idea junit

(已更新,以添加有关类的更多详细信息)

我是一名测试初学者,正在寻找一种转换方法。

我要测试的方法将转换一种类型的对象,然后返回另一种类型的对象,例如将UserOne对象转换为UserTwo对象,然后我要检查UserTwo对象的属性,以确保转换正确。

(更新)

我想做的是创建一个NewUser对象,将TBUser对象传递给原始的convert方法,然后测试该方法返回的对象,以确保转换正确。

我的问题是在哪里创建然后传递对象。

class TBUserStringToNew***UserConverterTest extends groovy.util.GroovyTestCase {
void setUp() {
    super.setUp();
    TBUser tbUser = new TBUser();
    tbUser.setContactNumber1("0123456789");
}

void tearDown() {
}

@Test
void testConvert() {
    New***User nUser = testConvert(tbUser);
    Assert.assertEquals("0123456789", nUser.getContactNumber());
}

NewUser类:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class NewUser implements Serializable {

@JsonProperty("ID")
private Integer id = 0;

@JsonProperty("eID")
private Integer eId;

@JsonProperty("Forename")
private String forename;

@JsonProperty("Surname")
private String surname;

@JsonProperty("DateOfBirth")
private String dateOfBirth;

@JsonProperty("AddressLineOne")
private String addressLineOne;

@JsonProperty("AddressLineTwo")
private String addressLineTwo;

@JsonProperty("PostCode")
private String postCode;

@JsonProperty("PostCodeArea")
private String postCodeArea;

@JsonProperty("ContactNumber")
private  String contactNumber;

@JsonProperty("EmailAddress")
private String emailAddress;

@JsonProperty("ReferralSource")
private String referralSource;

@JsonProperty("ReferralDetails")
private String referralDetails;

public Integer getId() {
    return id;
}

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

public Integer getEId() {
    return eId;
}

public void setEId(Integer eId) {
    this.eId = eId;
}

public String getForename() {
    return forename;
}

public void setForename(String forename) {
    this.forename = forename;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public String getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

public String getAddressLineOne() {
    return addressLineOne;
}

public void setAddressLineOne(String addressLineOne) {
    this.addressLineOne = addressLineOne;
}

public String getAddressLineTwo() {
    return addressLineTwo;
}

public void setAddressLineTwo(String addressLineTwo) {
    this.addressLineTwo = addressLineTwo;
}

public String getPostCode() {
    return postCode;
}

public void setPostCode(String postCode) {
    this.postCode = postCode;
}

public String getPostCodeArea() {
    return postCodeArea;
}

public void setPostCodeArea(String postCodeArea) {
    this.postCodeArea = postCodeArea;
}

public String getContactNumber() {
    return contactNumber;
}

public void setContactNumber(String contactNumber) {
    this.contactNumber = contactNumber;
}

public String getEmailAddress() {
    return emailAddress;
}

public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
}

public String getReferralSource() {
    return referralSource;
}

public void setReferralSource(String referralSource) {
    this.referralSource = referralSource;
}

public String getReferralDetails() {
    return referralDetails;
}

public void setReferralDetails(String referralDetails) {
    this.referralDetails = referralDetails;
}

TBUserStringToNewUserConverter类:

public class TBUserStringToNewUserConverter implements Converter<String, NewUser> {

protected static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private SimpleDateFormat nSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@Autowired
private ObjectMapper objectMapper;

@Autowired
private PlacesService placesService;

private NewUser convert(@NonNull TBUser tbUserIn) {

    ValidationErrors validationErrors = new ValidationErrors();

    String dob = null;

    try {
        dob = dayFormat.format(nlcSdf.parse(
                tbUserIn.getDob()
                .replace("T", " ")
                .replace("Z", "")));
    } catch (ParseException e) {
        ValidationError validationError = new ValidationError();
        validationError.setMessage(e.getLocalizedMessage());
        validationErrors.add(validationError);
    }

    NewUser nUser = new NewUser();
    nUser.setEId(tbUserIn.getId());
    nUser.setId(tbUserIn.getExternalId());
    nUser.setForename(tbUserIn.getFirstName());
    nUser.setSurname(tbUserIn.getSurname());
    nUser.setDateOfBirth(dob);
    nUser.setEmailAddress(tbUserIn.getEmail());
    nUser.setContactNumber(tbUserIn.getContactNumber1());

    TBAddress userAddress = tbUserIn.getAddress();

    String[] splitPostCode = userAddress.getPostzip().split("\\s+");
    nUser.setPostCodeArea(splitPostCode[0]);
    nUser.setPostCode(splitPostCode[1]);

    //standard source for all users
    nUser.setReferralSource("Healthy Living Centre");

    //placeholder referral details
    nUser.setReferralDetails("Referral Reasons: Weight Reduction, Mental Health, Respiratory, Other (Some other reason I entered). " +
            "\\r\\nMedical Conditions: Medical Conditions " +
            "\\r\\nMedications: Medications " +
            "\\r\\nReferral Source: Bob O Donnell\\r\\n(Doctor Medical) " +
            "\\r\\nRisks: May attempt something " +
            "\\r\\nOther Info: Other Relevant Info:");

    return nUser;
}

测试类

class TBUserStringToNewUserConverterTest extends groovy.util.GroovyTestCase {
void setUp() {
    super.setUp();
    TBUser tbUser = new TBUser();

}

void tearDown() {
}

@Test
void testConvert() {
    tbUser.setContactNumber1("");
    NewUser nUser = testConvert(tbUser);
    Assert.assertEquals("0123456789", nUser.getContactNumber());
}

2 个答案:

答案 0 :(得分:1)

乍一看,我认为您只是一步一步走,就像:

您的setUp()仅设置了最基本的东西,即tbUser对象:

void setUp() {
    super.setUp();
    TBUser tbUser = new TBUser();
}

然后,您需要对每个需要转换的属性进行一项测试。

@Test
void testConvert() {
    tbUser.setContactNumber1("0123456789");
    NewUser nUser = testConvert(tbUser);
    Assert.assertEquals("0123456789", nUser.getContactNumber());
}

就这么简单:一种需要转换的测试方法 per 属性。

当然,要使其正常运行,该tbUser必须是测试类的 field

class TBUserStringToNewUserConverterTest extends groovy.util.GroovyTestCase {

  private TbUser;

  void setUp() {
    tbUser = new TbUser();

...

但是请注意:这看起来仍然很奇怪。至少在使用JUnit时,您需要在应该在每次测试之前每次执行的任何方法上使用@Before批注。我对Groovy基类还不够熟悉。所以我只是希望上面的做对了。

答案 1 :(得分:0)

仅出于完整性的考虑而发布:在获得更多帮助之后,我需要公开convert(TBUser)方法,以便从Test类进行访问,并且需要创建converter类的实例,并使用该实例来访问convert方法,并传入TBUser,并接收返回的转换后的NewUser:

public class TBUserStringToNewUserConverterTest {

TBUser tbUser;
TBAddress tbAddress;
TBUserStringToNewUserConverter converter;

@Before
public void setUp() throws Exception {

    converter = new TBUserStringToNewUserConverter();

    tbUser = new TBUser();
    tbUser.setId(42);
    tbUser.setFirstName("Brian");
    tbUser.setSurname("Blessed");
    tbUser.setDob("2018-10-22 15:43:35");
    tbUser.setEmail("blessed@testmail.com");
    tbUser.setContactNumber1("0123456789");

    tbAddress = new TBAddress();
    tbAddress.setAddressLine1("41 John street");
    tbAddress.setAddressLine2("Flat Road");
    tbAddress.setId("Jst");
    tbAddress.setPostzip("G47 0JJ");
    tbAddress.setTown("Derry");
    tbAddress.setCountryCCA3("IRE");

    tbUser.setAddress(tbAddress);
}

@After
public void tearDown() throws Exception {
}

@Test
public void testConvert(){
    NewUser n = converter.convert(tbUser);
    Assert.assertNotNull(n);
    Assert.assertEquals("HLC", n.getReferralSource());
    Assert.assertEquals("0JJ", n.getPostCode());
    Assert.assertEquals("BT47", n.getPostCodeArea());
}