【问题标题】:JUnit test for getter method of custom object type自定义对象类型的getter方法的JUnit测试
【发布时间】:2017-07-10 15:37:46
【问题描述】:

我需要为我的方法编写单元测试。我遇到了一些麻烦,因为我是 JUnit 的新手。我需要为我创建的对象类型的 getter 方法编写测试。对象类型是UnitInfo,我需要为方法写一个测试

 @Override
public UnitInfo getInfo() {
    return info;
}

在教室里。我把我的建筑类、UnitInfo 类和我的 buildingTest 类放在下面的代码中。任何帮助表示赞赏。

package main.model.facility;

import java.util.List;

public class UnitInfo {
    private int capacity;
    private String name;
    private int idNumber;
    private List<String> details;

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(int idNumber) {
        this.idNumber = idNumber;
    }

    public List<String> getDetails() {
        return details;
    }

    public void setDetails(List<String> details) {
        this.details = details;
    }

    public void addDetail(String detail) {
        details.add(detail);
    }

    public void removeDetail(String detail) {
        details.remove(detail);
     }

}

建筑类:

 package main.model.facility;

    import java.util.List;

    public class Building extends Facility {

        private List<IFacility<UnitInfo>> subunits;
        private UnitInfo info;
        private ScheduleManager schedule;

        @Override
        public UnitInfo getInfo() {
            return info;
        }

        @Override
        public ScheduleManager getScheduleManager() {
            return schedule;
        }

        @Override
        public List<IFacility<UnitInfo>> listFacilities() {
            return subunits;
        }

        @Override
        public int requestAvailableCapacity() {
            int availableCapacity = 0;
            for (IFacility<UnitInfo> subunit : subunits){
                availableCapacity += subunit.requestAvailableCapacity();
            }
            return availableCapacity;
        }

    }

Junit

public class BuildingTest {
    Building defaultBuilding = new Building();
    ScheduleManager defaultSchedule = new ScheduleManager();

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGetInfo() { //this is the test I need to write
         Building b = defaultBuilding;
         assertEquals(b.getInfo(), null);

    }

@Test
public void testGetScheduleManager() {
    Building a = defaultBuilding; 
    assertEquals(a.getScheduleManager(), null);
}

【问题讨论】:

  • stackoverflow.com/a/21355383/1878022这个答案会帮助你
  • 仅作记录:从“良好设计”的角度来看:尝试使尽可能多的字段final。允许使用 setter 更改它们似乎很诱人;但很多时候它实际上有 no 值。因此,最好退后一步,仔细决定在创建对象时哪些信息应该是强制,并且不能更改。这减少了您必须测试的代码量!

标签: java unit-testing junit custom-object


【解决方案1】:

不要在类级别构建您的Building,而是在单元测试中创建它,以便每个测试都有自己的Building 可以使用。这样他们就不会互相干扰了。

您的测试目前似乎很好,当您构造新的Building 时,您没有初始化info,因此info 为空,并且您在单元测试中断言。你也可以使用assertNull(b.getInfo());

@Test
public void testGetInfo() { //this is the test I need to write
     Building b = new Building()
     assertEquals(b.getInfo(), null);
}

如果您确实将 info 初始化为其他内容,例如 info = new UnitInfo(),那么您可以将测试更改为:

@Test
public void testGetInfo() { //this is the test I need to write
     Building b = new Building()
     assertNotNull(b.getInfo());
}

如果您在创建新的Building 时初始化了info 并设置了一些字段,那会怎么样。

info = new UnitInfo();
info.setIdNumber(100);
info.setName("Some Unit Info");

然后在您的单元测试中,您可以断言这些字段已设置:

@Test
public void testGetInfo() { //this is the test I need to write
    Building b = new Building()
    assertNotNull(b.getInfo());
    assertEquals(b.getInfo().getIdNumber(), 100);
    assertEquals(b.getInfo().getName(), "Some Unit Info");
}

单元测试的想法是练习你的代码;调用你的方法,然后断言结果是你所期望的。一旦您对所有代码进行了良好的单元测试基础,您就可以对修改它充满信心,因为您知道测试会在您破坏某些内容时告诉您。

保持您的测试小而简单,不要做太多。只需进行设置,调用方法并确保结果正确。然后写一个新的测试,然后做点别的。不要编写做 5 种不同事情的测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2014-07-03
    相关资源
    最近更新 更多