How to mock a String using mockito?

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

    You can change your method to take the interface CharSequence:

    public final class A {
        public static String f(CharSequence str){
            try {
                return new String(str.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // This is the catch block that I want to exercise.
                ...
            }
        }
    }
    

    That way, you can still pass in String, but you can mock any way you like.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 01:07

    As others have indicated, you can't use Mockito to mock a final class. However, the more important point is that the test isn't especially useful because it's just demonstrating that String.getBytes() can throw an exception, which it can obviously do. If you feel strongly about testing this functionality, I guess you could add a parameter for the encoding to f() and send a bad value into the test.

    Also, you are causing the same problem for the caller of A.f() because A is final and f() is static.

    This article might be useful in convincing your coworkers to be less dogmatic about 100% code coverage: How to fail with 100% test coverage.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 01:11

    Mockito can't mock final classes. JMock, combined with a library from JDave can. Here are instructions.

    JMock doesn't do anything special for final classes other than rely on the JDave library to unfinalize everything in the JVM, so you could experiment with using JDave's unfinalizer and see if Mockito will then mock it.

    0 讨论(0)
  • 2021-02-05 01:11

    You will be testing code that can never be executed. UTF-8 support is required to be in every Java VM, see http://java.sun.com/javase/6/docs/api/java/nio/charset/Charset.html

    0 讨论(0)
提交回复
热议问题