Mockito when().thenReturn 没有给出预期的结果
Mockito when().thenReturn doesn't give expected result
我是 Junit 和 Mockito 的新手。
试图模拟 class 的对象之一,但它不起作用。
模拟方法是 returning 一个空列表,因此测试用例失败。
这是我写的代码。
Junit 测试 Class:这里我将对象和方法模拟为 return 一个 Arraylist,但是当执行代码时,这个模拟方法是 returning 一个空列表,由于哪个测试用例失败了。
package com.business;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import com.data.api.ToDoService;
public class TodoBusinessImplMockTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testRetrieveTodosRelatedToSpringUsingMock()
{
ToDoService todoServiceMock = mock(ToDoService.class);
List<String> todoList=Arrays.asList("Learn Spring MVC", "Learn Spring","Learn to Dance");
Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoServiceMock);
List<String> todos = todoBusinessImpl.retrieveTodosRelatedToSpring("Ranga");
assertEquals(2, todos.size());
}
}
接口:ToDoService.java
package com.data.api;
import java.util.List;
public interface ToDoService {
public List<String> retrieveTodos(String s);
}
TodoBusinessImpl.java
package com.business;
import java.util.ArrayList;
import java.util.List;
import com.data.api.ToDoService;
public class TodoBusinessImpl {
private ToDoService todoService;
TodoBusinessImpl(ToDoService todoService) {
this.todoService = todoService;
}
public List<String> retrieveTodosRelatedToSpring(String s) {
List<String> filteredTodos = new ArrayList<String>();
List<String> allTodos = todoService.retrieveTodos(s);
for (String todo : allTodos) {
if (todo.contains("Spring")) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
}
您的规格说明:
Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);
但您的通话使用:
todoBusinessImpl.retrieveTodosRelatedToSpring("Ranga");
“Ranga”不是“Dummy”,因此您的规格不匹配;因此 mockito returns 默认结果(这将是一个空列表)。
尝试将 Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);
中的 "Dummy"
替换为 anyString()
(import static org.mockito.ArgumentMatchers.anyString;
)。这对我有用。
我是 Junit 和 Mockito 的新手。 试图模拟 class 的对象之一,但它不起作用。 模拟方法是 returning 一个空列表,因此测试用例失败。 这是我写的代码。
Junit 测试 Class:这里我将对象和方法模拟为 return 一个 Arraylist,但是当执行代码时,这个模拟方法是 returning 一个空列表,由于哪个测试用例失败了。
package com.business;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import com.data.api.ToDoService;
public class TodoBusinessImplMockTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testRetrieveTodosRelatedToSpringUsingMock()
{
ToDoService todoServiceMock = mock(ToDoService.class);
List<String> todoList=Arrays.asList("Learn Spring MVC", "Learn Spring","Learn to Dance");
Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoServiceMock);
List<String> todos = todoBusinessImpl.retrieveTodosRelatedToSpring("Ranga");
assertEquals(2, todos.size());
}
}
接口:ToDoService.java
package com.data.api;
import java.util.List;
public interface ToDoService {
public List<String> retrieveTodos(String s);
}
TodoBusinessImpl.java
package com.business;
import java.util.ArrayList;
import java.util.List;
import com.data.api.ToDoService;
public class TodoBusinessImpl {
private ToDoService todoService;
TodoBusinessImpl(ToDoService todoService) {
this.todoService = todoService;
}
public List<String> retrieveTodosRelatedToSpring(String s) {
List<String> filteredTodos = new ArrayList<String>();
List<String> allTodos = todoService.retrieveTodos(s);
for (String todo : allTodos) {
if (todo.contains("Spring")) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
}
您的规格说明:
Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);
但您的通话使用:
todoBusinessImpl.retrieveTodosRelatedToSpring("Ranga");
“Ranga”不是“Dummy”,因此您的规格不匹配;因此 mockito returns 默认结果(这将是一个空列表)。
尝试将 Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);
中的 "Dummy"
替换为 anyString()
(import static org.mockito.ArgumentMatchers.anyString;
)。这对我有用。