gpt4 book ai didi

java - 为 DTO 构造函数创建 JUnit 测试

转载 作者:行者123 更新时间:2023-12-01 19:02:40 25 4
gpt4 key购买 nike

public InventoryTable(String terminalId, String machineType, String machineName) {
super();
this.terminalId = terminalId;
this.machineType = machineType;
this.machineName = machineName;
}

public InventoryTable(InventoryTableDTO inventoryDTO) {
this.terminalId = inventoryDTO.getTerminalId();
this.machineType = inventoryDTO.getMachineType();
this.machineName = inventoryDTO.getMachineName();

}

这是我的构造函数,我需要为第二个构造函数创建一个 JUnit 测试

下面是我对第一个构造函数的 JUnit 测试

@Before
public void setUp() {
inventoryTable = new InventoryTable("12345", "TypeMoTo", "Machina");
inventoryTable.setTerminalId("12345");
inventoryTable.setMachineType("TypeMoTo");
inventoryTable.setMachineName("Machina");
}

@Test
public void testThis() {
assertThat(inventoryTable.getTerminalId()).isEqualTo("12345");
assertThat(inventoryTable.getMachineType()).isEqualTo("TypeMoTo");
assertThat(inventoryTable.getMachineName()).isEqualTo("Machina");

}

谢谢:)

最佳答案

@Before
public void setUp() {
inventoryTable = new InventoryTable("12345", "TypeMoTo", "Machina");
}
@Test
public void testCopyConstructor() {
InventoryTable it = new InventoryTable(inventoryTable);
assertThat(inventoryTable.getTerminalId()).isEqualTo("12345");
assertThat(inventoryTable.getMachineType()).isEqualTo("TypeMoTo");
assertThat(inventoryTable.getMachineName()).isEqualTo("Machina");

}

第二个构造函数是复制构造函数在设置中,您已经有一个实例,因此您可以将其传递给复制构造函数并断言值。

关于java - 为 DTO 构造函数创建 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com