简体   繁体   English

为 DTO 构造函数创建 JUnit 测试

[英]Create JUnit Test for a DTO Constructor

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();

        }

Here's my constructors and i need to create a JUnit Test for the second one这是我的构造函数,我需要为第二个构造函数创建一个 JUnit 测试

Below is my JUnit test for the first constructor下面是我对第一个构造函数的 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");

    }

Thank you :)谢谢 :)

@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");

}

Second constructor is Copy Constructor In setup you already have one instance, so You can pass it to Copy Constructor and asserts values.第二个构造函数是 Copy Constructor在设置中,您已经有一个实例,因此您可以将其传递给 Copy Constructor 并断言值。

Same as you are doing for 1st one, here you can use old object to create new InventoryTable for 2nd constructor.与您为第一个所做的相同,在这里您可以使用旧对象为第二个构造函数创建新的InventoryTable

@Test
public void testThis2() {
   InventoryTable inventoryNew = new InventoryTable();
   assertThat(inventoryNew.getTerminalId()).isEqualTo(inventoryTable.getTerminalId());
   assertThat(inventoryNew.getMachineType()).isEqualTo(inventoryTable.getMachineType());
   assertThat(inventoryNew.getMachineName()).isEqualTo(inventoryTable.getMachineName());  
}

In general you can do:一般来说,你可以这样做:

@Test
public void testSecondConstructor() {
   // setup:
   InventoryTableDTO dto =Mockito.mock(InventoryTableDTO.class);
   Mockito.when(dto.getTerminalId()).thenReturn("12345");
   Mockito.when(dto.getMachineType()).thenReturn("TypeMoTo");
   Mockito.when(dto.getMachineName()).thenReturn("Machina");


   // when:
   InventoryTable underTest = new InventoryTable(dto);

   // then:
   assertThat(inventoryTable.getTerminalId()).isEqualTo("12345");
   assertThat(inventoryTable.getMachineType()).isEqualTo("TypeMoTo");
   assertThat(inventoryTable.getMachineName()).isEqualTo("Machina");

}

Alternatively to creating mock with mockito you can also create InventoryTableDTO with its constructor.除了使用 mockito 创建模拟,您还可以使用其构造函数创建InventoryTableDTO

First of all I think that you should just get rid of your @Before because tested object is initialized differently depending on the case and init the cases inside tests.首先,我认为您应该摆脱@Before因为测试对象的初始化方式因案例而异,并在测试中初始化案例。 But first also a small change in your 2nd constructor, to use the 1st:但首先在您的第二个构造函数中进行一个小的更改,以使用第一个:

public InventoryTable(InventoryTableDTO inventoryDTO) {
    this(inventoryDTO.getTerminalId(),
        inventoryDTO.getMachineType(),
        inventoryDTO.getMachineName());
}

Tests:测试:

@Test
public void test1st() {
    InventoryTable inventoryTable =
            new InventoryTable("12345", "TypeMoTo", "Machina");        
    assertThat(inventoryTable.getTerminalId()).isEqualTo("12345");
    assertThat(inventoryTable.getMachineType()).isEqualTo("TypeMoTo");
    assertThat(inventoryTable.getMachineName()).isEqualTo("Machina");

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM