我正在关注BDD apprach并使用黄瓜进行单元测试 我有一个课程,如下所示
public class EmployeeServiceImpl {
private static Log log = LogFactory.getlog(EmployeeServiceImpl.class);
@Autowired
private EmployeeDao employeeDao;
public void saveEmployee(Employee emp) throws Exception {
try {
employeeDao.saveemployee(emp);
} catch (Exception ex) {
log.error("Error occured "+ex);
}
}
}
任何人都可以帮助我如何为上面的代码片段编写异常场景?
答案 0 :(得分:2)
我将使用的算法是这样的:
我会使用Mockito来验证互动,http://site.mockito.org/#how
JUnit解决方案可能如下所示:
@Test
public void verify_logging() throws Exception {
Log log = mock(Log.class);
Employee emp = new Employee();
saveEmployee(emp);
verify(log, times(1)).error("Error occured");
}
通过三个步骤将其转换为Cucumber将是这样的:
private Log log;
private Employee emp;
@Given("prepare employee")
public void given() {
Log log = mock(Log.class);
Employee emp = new Employee();
}
@When("save employee")
public void when() throws Exception {
saveEmployee(emp);
}
@Then("exception should be logged")
public void then() {
verify(log, times(1)).error("Error occured");
}
您的任务是为步骤方法提供更好的名称以及更好的步骤。