2024-04-29
其它
0

image-20240429152137163

image-20240429152152388

image-20240429152159913

image-20240429152207921

image-20240429152215459

2023-12-20
单元测试
0

什么是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)); }
2023-12-19
Paddle
0

ai-paddle部署

关键词:[[2023 id=643c3078-da79-43c8

2023-11-29
单元测试
0

pom

xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> </dependencies>

Mock List

java
public class ListTest { @Test public void letsMockListSize() { List list = mock(List.class); when(list.size()).thenReturn(10); assertEquals(10, list.size()); } @Test public void letsMockListSizeWithMultipleReturnValues() { List list = mock(List.class); when(list.size()).thenReturn(10).thenReturn(20); assertEquals(10, list.size()); // First Call assertEquals(20, list.size()); // Second Call } @Test public void letsMockListGet() { List<String> list = mock(List.class); when(list.get(0)).thenReturn("in28Minutes"); assertEquals("in28Minutes", list.get(0)); assertNull(list.get(1)); } @Test(expected = RuntimeException.class) public void letsMockListGetToThrowException() { List<String> list = mock(List.class); when(list.get(Mockito.anyInt())).thenThrow( new RuntimeException("Something went wrong")); list.get(0); } @Test public void letsMockListGetWithAny() { List<String> list = mock(List.class); Mockito.when(list.get(Mockito.anyInt())).thenReturn("in28Minutes"); // If you are using argument matchers, all arguments // have to be provided by matchers. assertEquals("in28Minutes", list.get(0)); assertEquals("in28Minutes", list.get(1)); } @Test public void bddAliases_UsingGivenWillReturn() { List<String> list = mock(List.class); //given given(list.get(Mockito.anyInt())).willReturn("in28Minutes"); //then assertThat("in28Minutes", is(list.get(0))); assertThat("in28Minutes", is(list.get(0))); } }

Verify

见转载


Capture Argument


java
@Test public void captureArgument() { ArgumentCaptor<String> argumentCaptor = ArgumentCaptor .forClass(String.class); TodoService todoService = mock(TodoService.class); List<String> allTodos = Arrays.asList("Learn Spring MVC", "Learn Spring", "Learn to Dance"); Mockito.when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos); TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService); todoBusinessImpl.deleteTodosNotRelatedToSpring("Ranga"); Mockito.verify(todoService).deleteTodo(argumentCaptor.capture()); assertEquals("Learn to Dance", argumentCaptor.getValue()); }

Hamcrest Matchers


xml
<dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency>

java
import static org.hamcrest.CoreMatchers.hasItems; assertThat(scores, hasSize(4)); assertThat(scores, hasItems(100, 101)); assertThat(scores, everyItem(greaterThan(90))); assertThat(scores, everyItem(lessThan(200))); // String assertThat("", isEmptyString()); assertThat(null, isEmptyOrNullString());