处于调试模式的Mockito的断点结果为WrongTypeOfReturnValue

时间:2018-11-06 17:13:44

标签: intellij-idea mockito

有一个简单的代码段。

@Component
public class SomeDependency {
    public Optional<Integer> getSomeInt(String string) {
        return Optional.of(1);
    }
}

@Component
public class SomeService {

    @Autowired
    private SomeDependency someDependency;

    public String someMethod(String string) {
        return String.valueOf(someDependency.getSomeInt(string).get());
    }
}

@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {

    @Mock
    private SomeDependency someDependency;

    @InjectMocks
    private SomeService someService;

    @Test
    public void test() {
        when(someDependency.getSomeInt(anyString()))
                .thenReturn(Optional.of(111));

        String value = someService.someMethod("test");

        assertThat(value, is("111"));
    }
}

现在,当我运行测试时,它运行正常,但是当我在调试模式下运行它,同时在……...然后返回...模拟并使用step over时具有断点时,会引发以下错误。 / p>

org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
        Optional cannot be returned by toString()
        toString() should return String
        ***
        If you're unsure why you're getting above error read on.
        Due to the nature of the syntax above problem might occur because:
        1. This exception *might* occur in wrongly written multi-threaded tests.
        Please refer to Mockito FAQ on limitations of concurrency testing.
        2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
        - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

但是,如果我使用以下oneliner代替twoliner,那就可以了。没有错误返回。

when(someDependency.getSomeInt(anyString())).thenReturn(Optional.of(111));

那么,问题出在哪里?

1 个答案:

答案 0 :(得分:0)

您是否使用想法进行调试?创意调试器可能会中断存根过程。

转到首选项->构建,执行,部署->调试器->数据视图-> Java ,取消启用'toString()'对象视图复选框