How to mock a String using mockito?

后端 未结 15 1782
佛祖请我去吃肉
佛祖请我去吃肉 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:11

    If all you are going to do in your catch block is throw a runtime exception then you can save yourself some typing by just using a Charset object to specify your character set name.

    public final class A{
        public static String f(String str){
            return new String(str.getBytes(Charset.forName("UTF-8")));
        }
    }
    

    This way you aren't catching an exception that will never happen just because the compiler tells you to.

提交回复
热议问题