如何在XML文件中模拟数据库中的布尔值

时间:2016-03-04 09:25:40

标签: java xml unit-testing

我在XML文件中设置布尔值时遇到问题。 让我首先展示代码:

首先是课程:

@MappedSuperclass
@Inheritance
public abstract class MeterAbstract {

    @Id
    @GeneratedValue
    @Column
    protected int id;
    @Column(nullable = false)
    @NotEmpty(message="Wprowadź wartość")
    protected String description;
    @Column(nullable = false, unique = true)
    @NotEmpty(message="Wprowadź wartość")
    protected String serialNumber;
    @Column(nullable = false)
    @NotEmpty(message="Wprowadź wartość")
    protected String unit;
    @ManyToOne
    protected Apartment apartment;

    @Autowired
    public MeterAbstract(String description, String serialNumber,
            String unit, Apartment apartment) {
        super();
        this.description = description;
        this.serialNumber = serialNumber;
        this.unit = unit;
        this.apartment = apartment;

    }

    public MeterAbstract() {
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    @Override
    public String toString() {
        String opisMieszkania;
        try {
            opisMieszkania = apartment.getDescription();
        } catch (NullPointerException e) {
            // TODO Auto-generated catch block
            opisMieszkania = "brak";
        }
        return "\n" + description + ", serialNumber:" + serialNumber
                + ", Mieszkanie:" + opisMieszkania;
    }


    public Apartment getApartment() {
        return apartment;
    }

    public void setApartment(Apartment apartment) {
        this.apartment = apartment;
    }

    public int getId() {
        return id;
    }

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


}

现在我尝试在db中模拟的扩展类:

@Entity
@Table
public class MeterGas extends MeterAbstract {
    @Column(nullable=false)
    private boolean cwu;

    @Autowired
    public MeterGas(String description, String serialNumber, String unit, Apartment apartment, boolean isCWU) {
        super(description, serialNumber, unit, apartment);
        this.cwu = isCWU;
    }

    public MeterGas() {
        super.setUnit("m3");
        this.cwu = false;
    }


    public String toString() {
        String apartment;
        try {
            apartment = this.apartment.getDescription();
        } catch (NullPointerException e) {
            apartment = "brak";
        }
        return "\n" + description + ", nrSeryjnyLicznika:" + serialNumber
                + ", Mieszkanie:" + apartment + ", CWU: " + cwu;
    }

    public boolean isCwu() {
        return cwu;
    }

    public void setCwu(boolean cwu) {
        this.cwu = cwu;
    }

测试看起来像这样:

public class MeterGasDaoImplTest extends EntityDaoImplTest{


    @Autowired
    MeterGasDAO meterDao;


    @Override
    protected IDataSet getDataSet() throws Exception {
        IDataSet[] datasets = new IDataSet[] {
                new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("Apartment.xml")),
                new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("MeterGas.xml")) };
        return new CompositeDataSet(datasets);
    }

    @Test
    public void findById() {
        Assert.assertNotNull(meterDao.getById(1));
        Assert.assertNull(meterDao.getById(4));
    }

    @Test
    public void save() {
        meterDao.save(getSampleMeter());
        Assert.assertEquals(meterDao.getList().size(), 4);
    }

    @Test
    public void deleteById() {
        meterDao.deleteGasByID(1);
        Assert.assertEquals(meterDao.getList().size(), 2);
    }

    @Test
    public void deletetByInvalidId() {
        meterDao.deleteGasByID(9);
        Assert.assertEquals(meterDao.getList().size(), 3);
    }

    @Test
    public void findAll() {
        Assert.assertEquals(meterDao.getList().size(), 3);
    }

    @Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
    public void saveDuplicate() {
        meterDao.save(getDuplcateNubmerApartment());
        Assert.assertEquals(meterDao.getList().size(), 4);
    }

    public MeterGas getSampleMeter() {
        MeterGas meter = new MeterGas();
        meter.setApartment(null);
        meter.setDescription("test");
        meter.setSerialNumber("7676434211");
        meter.setUnit("test");
        return meter;
    }

    public MeterGas getDuplcateNubmerApartment() {
        MeterGas meter = new MeterGas();
        meter.setApartment(null);
        meter.setDescription("test");
        meter.setSerialNumber("1");
        meter.setUnit("test");
        return meter;
    }

最后是XML:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <meterGas id="1" description="glowny" serialNumber="1" unit="test"
         CWU="true" />
    <meterGas id="2" description="cz wpolna" serialNumber="2"
        unit="test" apartment_id="true"   CWU="true" />
    <meterGas id="3" description="piwnica" serialNumber="3" unit="test"
        apartment_id="2"  CWU="true"  />
    <meterGas id="4" description="parter" serialNumber="4" unit="test"
        apartment_id="3"   CWU="true" />
    <meterGas id="5" description="CWU" serialNumber="5" unit="test"
        apartment_id="1" CWU="true"  />
</dataset>

问题是它不接受CWU。当我进行测试时,我得到:

  

org.dbunit.dataset.NoSuchColumnException:meterGas.cwu

我已经尝试了上下两种情况(CWU,cwu)而不是true / false我放了0/1但是这些组合似乎都没有用。

我需要这个列,因为它设置为nullable = false所以如果我把它留下来我得到:

  

org.h2.jdbc.JdbcSQLException:列“CWU”不允许NULL; SQL   声明:插入METERGAS(ID,DESCRIPTION,SERIALNUMBER,UNIT)   值(?,?,?,?)[23502-187]

我已经为其他课程做了一些测试,他们工作正常。我试着谷歌答案,但没有发现任何帮助。

0 个答案:

没有答案