JUNIT testing void methods

后端 未结 7 904
遇见更好的自我
遇见更好的自我 2020-11-27 02:41

I have a java class full of void methods, and I want to make some unit test to get maximal code coverage.

For example I have this method :

protected          


        
相关标签:
7条回答
  • 2020-11-27 03:17

    If your method is void and you want to check for an exception, you could use expected: https://weblogs.java.net/blog/johnsmart/archive/2009/09/27/testing-exceptions-junit-47

    0 讨论(0)
  • 2020-11-27 03:20

    If your method has no side effects, and doesn't return anything, then it's not doing anything.

    If your method does some computation and returns the result of that computation, you can obviously enough assert that the result returned is correct.

    If your code doesn't return anything but does have side effects, you can call the code and then assert that the correct side effects have happened. What the side effects are will determine how you do the checks.

    In your example, you are calling static methods from your non-returning functions, which makes it tricky unless you can inspect that the result of all those static methods are correct. A better way - from a testing point of view - is to inject actual objects in that you call methods on. You can then use something like EasyMock or Mockito to create a Mock Object in your unit test, and inject the mock object into the class. The Mock Object then lets you assert that the correct functions were called, with the correct values and in the correct order.

    For example:

    private ErrorFile errorFile;
    
    public void setErrorFile(ErrorFile errorFile) {
        this.errorFile = errorFile;
    }
    
    private void method1(arg1) {
        if (arg1.indexOf("$") == -1) {
    
            //Add an error message 
            errorFile.addErrorMessage("There is a dollar sign in the specified parameter");
        }
    }
    

    Then in your test you can write:

    public void testMethod1() {
        ErrorFile errorFile = EasyMock.createMock(ErrorFile.class);
        errorFile.addErrorMessage("There is a dollar sign in the specified parameter");
        EasyMock.expectLastCall(errorFile);
        EasyMock.replay(errorFile);
    
        ClassToTest classToTest = new ClassToTest();
        classToTest.setErrorFile(errorFile);
        classToTest.method1("a$b");
    
        EasyMock.verify(errorFile); // This will fail the test if the required addErrorMessage call didn't happen
    }
    
    0 讨论(0)
  • 2020-11-27 03:22

    You can learn something called "mocking". You can use this, for example, to check if: - a function was called - a function was called x times - a function was called at least x times - a function was called with a specific set of parameters. In your case, for example, you can use mocking to check that method3 was called once with whatever you pass as arg1 and arg2.

    Have a look at these: https://code.google.com/p/mockito/ https://code.google.com/p/powermock/

    0 讨论(0)
  • 2020-11-27 03:22

    I think you should avoid writing side-effecting method. Return true or false from your method and you can check these methods in unit tests.

    0 讨论(0)
  • 2020-11-27 03:23

    You can still unit test a void method by asserting that it had the appropriate side effect. In your method1 example, your unit test might look something like:

    public void checkIfValidElementsWithDollarSign() {
        checkIfValidElement("$",19);
        assert ErrorFile.errorMessages.contains("There is a dollar sign in the specified parameter");
    }
    
    0 讨论(0)
  • 2020-11-27 03:26

    I want to make some unit test to get maximal code coverage

    Code coverage should never be the goal of writing unit tests. You should write unit tests to prove that your code is correct, or help you design it better, or help someone else understand what the code is meant to do.

    but I dont see how I can test my method checkIfValidElements, it returns nothing or change nothing.

    Well you should probably give a few tests, which between them check that all 7 methods are called appropriately - both with an invalid argument and with a valid argument, checking the results of ErrorFile each time.

    For example, suppose someone removed the call to:

    method4(arg1, arg2);
    

    ... or accidentally changed the argument order:

    method4(arg2, arg1);
    

    How would you notice those problems? Go from that, and design tests to prove it.

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