How to mock a String using mockito?

后端 未结 15 1781
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 00:22

I need to simulate a test scenario in which I call the getBytes() method of a String object and I get an UnsupportedEncodingException.

I have tried to achie

15条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 01:06

    If you have a block of code that can never actually be run, and a managerial requirement to have 100% test coverage, then something's going to have to change.

    What you could do is make the character encoding a member variable, and add a package-private constructor to your class that lets you pass it in. In your unit test, you could call the new constructor, with a nonsense value for the character encoding.

提交回复
热议问题