我需要为我的方法编写单元测试。我遇到了一些麻烦,因为我是 JUnit 新手。我需要为我创建的对象类型的 getter 方法编写一个测试。对象类型是UnitInfo,我需要为该方法编写一个测试
@Override
public UnitInfo getInfo() {
return info;
}
在类(class)楼里。我将我的构建类、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;
}
}
朱尼特
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);
}
最佳答案
不要在类级别构建您的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());
}
如果您在创建新的建筑物
时初始化了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 件不同事情的测试。
关于java - JUnit 测试自定义对象类型的 getter 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360129/