我正在尝试为方法插入JU​​nit测试用例,以插入将参数作为雇员ID的时间表。我应该如何尝试?

时间:2019-07-02 02:52:40

标签: java api unit-testing testing junit

我试图在时间表类型中创建一个新对象并分配值。但是我有一个方法调用insertTimesheet,它需要有employeeId。由于时间表对象中的employeeId没有此类值,因此测试失败。

@Test
public void testInsertTimesheet() {
    Timesheet expectedToSave = new Timesheet( 1001 ,"2019-01-10", "2019-01-05", "comment section is this ", "saved", null, null);  
    int actualInsertData = timesheetRepository.insertTimesheet(expectedToSave, 100789);
    Assertions.assertEquals(expectedToSave, actualInsertData);
}

1 个答案:

答案 0 :(得分:0)

JdbcTemplate.update()方法将返回生效的行数。如果未成功插入,将抛出DataAccessException异常。

@Test
public void testInsertTimesheet() {
    Timesheet expectedToSave = new Timesheet( 1001 ,"2019-01-10", "2019-01-05", "comment section is this ", "saved", null, null);  
    int actualInsertData = timesheetRepository.insertTimesheet(expectedToSave, 100789); //In here number of effected rows is 1 so insertTimeSheet() will return 1
    Assert.assertEquals(1, actualInsertData); //expected value is 1
}