2023-12-20
单元测试
0
请注意,本文编写于 611 天前,最后修改于 609 天前,其中某些信息可能已经过时。

什么是BDD?

behavior-driven development (BDD),行为驱动开发。

参考:https://en.wikipedia.org/wiki/Behavior-driven_development

语法区别:

given-willReturn when-thenReturn stub-toReturn

BDDMockito优点:易于阅读

java
@Test public void usingMockito() { TodoService todoService = mock(TodoService.class); List<String> allTodos = Arrays.asList("Learn Spring MVC", "Learn Spring", "Learn to Dance"); when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos); TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService); List<String> todos = todoBusinessImpl .retrieveTodosRelatedToSpring("Ranga"); assertEquals(2, todos.size()); } @Test public void usingMockito_UsingBDD() { TodoService todoService = mock(TodoService.class); TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService); List<String> allTodos = Arrays.asList("Learn Spring MVC", "Learn Spring", "Learn to Dance"); //Given - setup given(todoService.retrieveTodos("Ranga")).willReturn(allTodos); //When - actual method call List<String> todos = todoBusinessImpl .retrieveTodosRelatedToSpring("Ranga"); //Then - asserts assertThat(todos.size(), is(2)); }

本文作者:问海

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!