Junit Mockito when/then结果仍在传递给方法
我对莫基托有了意想不到的行为 我在下面的代码中犯了一个错误,我应该将TodoService放在when/then中。 但出于某种原因,下面的测试通过了 我希望它会失败,因为根据when/then条件,filteredTodos应该等于todosJunit Mockito when/then结果仍在传递给方法,junit,mockito,Junit,Mockito,我对莫基托有了意想不到的行为 我在下面的代码中犯了一个错误,我应该将TodoService放在when/then中。 但出于某种原因,下面的测试通过了 我希望它会失败,因为根据when/then条件,filteredTodos应该等于todos @Test public void testRetrieveTodosRelatedToSpring_checkContent() { TodoService mockTodoServiceStub = mock(TodoService.clas
@Test
public void testRetrieveTodosRelatedToSpring_checkContent() {
TodoService mockTodoServiceStub = mock(TodoService.class);
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(mockTodoServiceStub);
List<String> todos = Arrays.asList
("1","2","Learn Spring MVC", "Learn Spring", "Learn to Dance");
when(todoBusinessImpl.retrieveTodosRelatedToSpring("Dummy")).thenReturn(todos);
List<String> filteredTodos = todoBusinessImpl.retrieveTodosRelatedToSpring("Dummy");
assertArrayEquals(new String[]{"Learn Spring MVC", "Learn Spring"}, filteredTodos.toArray());
}
@测试
public void testretrievetodosratedtostring_checkContent(){
TodoService mockTodoServiceStub=mock(TodoService.class);
TodoBusinessImpl TodoBusinessImpl=新的TodoBusinessImpl(mockTodoServiceStub);
List todos=Arrays.asList
(“1”、“2”、“学春天MVC”、“学春天”、“学跳舞”);
当(ToDobBusinessImpl.retrieveTodosRelatedToSpring(“虚拟”))时,然后返回(todos);
List filteredTodos=todoBusinessImpl.retrieveTodosRelatedToSpring(“虚拟”);
AssertArrayQuals(新字符串[]{“Learn Spring MVC”,“Learn Spring”},filteredTodos.toArray());
}
以下是测试使用的其他类:
public class TodoBusinessImpl {
private TodoService todoService;
public TodoBusinessImpl(TodoService todoService) {
super();
this.todoService = todoService;
}
public List<String> retrieveTodosRelatedToSpring(String user) {
List<String> filteredTodos = new ArrayList<String>();
List<String> todos = todoService.retrieveTodos(user);
for (String todo : todos) {
if(todo.contains("Spring")) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
public类到业务impl{
私有TodoService TodoService;
公共TodoBusinessImpl(TodoService TodoService){
超级();
this.todoService=todoService;
}
公共列表检索TodosRelatedToString(字符串用户){
List filteredTodos=new ArrayList();
List todos=todoService.retrieveTodos(用户);
for(字符串todo:todos){
if(todo.contains(“Spring”)){
filteredTodos.add(todo);
}
}
返回filteredTodos;
}
}
这是todoservice接口(没有实现)
公共服务接口{
公共列表检索路径(字符串用户);
}
有人能解释一下为什么这个测试仍然通过吗?多谢各位
public interface TodoService {
public List<String> retrieveTodos(String user);
}