org.mockito.exceptions.misusing.WrongTypeOfReturnValue:getDetails()无法返回ArrayList

时间:2018-06-02 07:27:50

标签: java mockito junit4

这是代码

public class TodoBusinessImpl {
        BusinessManager businessManager;

        public TodoBusinessImpl(BusinessManager businessManager) {
            this.businessManager = businessManager;
        }

        public List<String> retrieveTodosRelatedToSpring(String user) {
            List<String> filteredTodos = new ArrayList<String>();
            Map<String, String> test = businessManager.getDetails();
            return filteredTodos;
        }
    }
  

那有什么问题?我做错了什么

public class MockitoTest {  
    private BusinessManager businessManager;    
    @Before
    public void setUp() {
        businessManager = Mockito.mock(BusinessManager.class);
    }   
    @Test
    public void retrieveTodosRelatedToSpringTest() {
        TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(businessManager);
        Mockito.when(todoBusinessImpl.retrieveTodosRelatedToSpring("test"))
    .thenReturn(Arrays.asList("test"));
    }
}

这是测试类。获得org.mockito.exceptions.misusing.WrongTypeOfReturnValue的异常: getDetails()

不能返回ArrayList

1 个答案:

答案 0 :(得分:0)

您必须调用要测试的方法,不要模拟它的行为“在调用时不告诉它做什么”。 代替:

Mockito.when(todoBusinessImpl.retrieveTodosRelatedToSpring("test")).thenReturn(Arrays.asList("test"));

您应该这样做:

 List<String> list = todoBusinessImpl.retrieveTodosRelatedToSpring("test");
 // print list or assert it's content to make sure you're getting the right response