使用getter和setter实施JUnit测试

时间:2018-10-05 07:21:18

标签: java junit

我想创建JUnit测试来测试JAXB代码:

@XmlRootElement(name = "reconcile")
public class Reconcile {

    @XmlElement(name = "start_date")
    @XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
    public LocalDateTime start_date;
    @XmlElement(name = "end_date")
    @XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
    public LocalDateTime end_date;
    @XmlElement(name = "page")
    public String page;

    //// getters and setters
}

我使用Java10。我尝试了上述代码的JUnit测试:

import java.time.LocalDateTime;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.datalis.plugin.reconcile.Reconcile;
import org.junit.jupiter.api.Test;

public class ReconciliationTest {

    @Test
    public void uniqueTransactionIdLenght() throws JAXBException {

        Reconcile reconcile = new Reconcile();
        reconcile.start_date = LocalDateTime.of(2018, 4, 8, 11, 2, 44);
        reconcile.end_date = LocalDateTime.of(2018, 11, 8, 11, 2, 44);
        reconcile.page = "1";

        JAXBContext jaxbContext = JAXBContext.newInstance(Reconcile.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(reconcile, System.out);
    }
}

但是当我运行代码时,我得到了:

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions
    at org.datalis.plugin.jaxb.ReconciliationTest.uniqueTransactionIdLenght(ReconciliationTest.java:22)

在此行

JAXBContext jaxbContext = JAXBContext.newInstance(Reconcile.class);

当我从Java主类中删除getter和setter时,它的工作正常。有什么办法可以解决这个问题?

解决方案:

我在此处添加了@XmlAccessorType(XmlAccessType.FIELD):

@XmlRootElement(name = "reconcile")
@XmlAccessorType(XmlAccessType.FIELD)
public class Reconcile {
...
}

0 个答案:

没有答案