0

I need to write unit test for some static void methods with an unknown/unpredictable side effect. For example

public void doSth() {
    try {
        HttpPostUtil.sendRequest("abc", "xyz");
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

HttpPostUtil is in another jar file. It is out of my control and it will post the data to some web services . What is the testing methodology that we could do in this situation? (PowerMockito is not allowed here :()

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • 1
    There's more or less nothing you can do. If you can't instrument the code, you can't intercept the `sendRequest` call. At most you could change `System.err` and intercept the `printStackTrace`, but your HTTP call would have to fail somehow and throw an exception. – Sotirios Delimanolis Feb 12 '15 at 16:22
  • I don't understand why PowerMockito is restricted (in fact, you don't even need it if you're already using Mockito as you tagged it in the question). Anyway, it depends on what you want/need to mock and what you have to test. Since you haven't provided that exact info, it's really hard to understand what you want/need to accomplish. – Luiggi Mendoza Feb 12 '15 at 16:23
  • @LuiggiMendoza With the `PowerMockito`, it will be easy to verify on the static method – Thai Tran Feb 12 '15 at 16:24
  • @ThaiTran PowerMockito is the mix of two frameworks: PowerMock and Mockito. You can do this using Mockito only (no need of PowerMock nor PowerMockito). If you can't use any mock framework **at all**, then you cannot do your unit testing properly for this case. – Luiggi Mendoza Feb 12 '15 at 16:25
  • possible duplicate of [How to test void method with Junit testing tools?](http://stackoverflow.com/questions/1244541/how-to-test-void-method-with-junit-testing-tools) – kryger Feb 12 '15 at 16:51
  • Not sure why people vote for closing this question. The possible duplicated question is all about how to test void method with a CLEAR side effect, so the way to test is quite obvious – Thai Tran Feb 14 '15 at 03:44

2 Answers2

4

Because it is void method the only thing you can test is behaviour. In such situation you have few options

  1. Wrap calls to HttpPostUtil with an object in order to not have it as a static and mock it in tests (by mockito or by injection of your implementation into your object). This is called sprout class.
  2. Wrap call to HttpPostUtil into the method and override it in test suite to something else.

Generally speaking - if it is hard to test -> it is hard to use -> implementation is bad -> so it needs refactoring, not tuning the tests.

Gaskoin
  • 2,469
  • 13
  • 22
  • I like your answer. It is true that when the class is hard to test, then the implementation is bad and need to be refactored. In fact, we are trying to do a lot of refactoring now with removing `static` methods and put more injections to the class. Upvote ! – Thai Tran Feb 14 '15 at 03:41
0

While testing a method, you should always just test that method and mock the other calls to other methods. Because the other methods should have been tested in their own test methods.

Here you simply need 2 tests if you also want to test the catch case. With Mockito:

@InjectMocks
private YourClass yourClass;

@Mock
private HttpPostUtil httpPostUtil;

@Test
public void testDoSthPositive() {
    // Run Test
    yourClass.doSth();

    // Control
    verify(httpPostUtil).sendRequest("abc", "xyz");
    verifyNoMoreInteractions(httpPostUtil);  // Optional
}

@Test
public void testDoSthNegative() {
    // Preparation
    NullPointerException exceptionMock = mock(NullPointerException.class);
    doThrow(exceptionMock).when(httpPostUtil).sendRequest("abc", "xyz");

    // Run Test
    yourClass.doSth();

    // Control
    verify(exceptionMock).printStackTrace();
    verifyNoMoreInteractions(exceptionMock, httpPostUtil);  // Optional
}
akcasoy
  • 6,497
  • 13
  • 56
  • 100
  • Please read the question carefully, `HttpPostUtil` is calling a static method – Thai Tran Feb 14 '15 at 03:30
  • sorry but where did you write that that a static method is? Do you expect people conclude that from the first capital or what? – akcasoy Feb 14 '15 at 09:22
  • Sorry, I should make it clearer. I thought that everyone should know that when StackOverflow highlights the word, then that word is supposed to be a class not an object ;). Anyway, fixed !!! – Thai Tran Feb 14 '15 at 12:35