1

Suppose that I have a class like;

public class FooBar {

    public int getMethod(List<String> code){

        if(code.size() > 100)
            throw new Exception;

            return 0;
    }
}

and I have a test class like this;

@RunWith(PowerMockRunner.class)
@PrepareForTest(FooBar.class)
public class FooBarTest{

    FooBar fooBarInstance;

    @Before
    public void setUp() {

        //MockitoAnnotations.initMocks(this);
        fooBarInstance = new FooBar();   
    }

    @Test(expected = Exception.class)
    public void testGetCorrelationListCodesParameter() {

        List<String> codes = Mockito.spy(new ArrayList<String>());
        Mockito.doReturn(150).when(codes).size();
        fooBarInstance.getMethod(codes);
    }
}

How can I make this test method to throw an exception ? I've dealing for hours to do this. Well thanks anyway.

4
  • What call exactly should throw the exception? Jan 21, 2014 at 15:48
  • the codes.size() should return more than 100 ?
    – quartaela
    Jan 21, 2014 at 15:50
  • Have you tried it without Powermock? Or when(codes.size()).thenReturn(150). And why don't you use a simple mock of List instead of a spy? Jan 21, 2014 at 16:45
  • the thing is I have other tests to finish and I generally use PowerMockRunner and I have to say that I'm a newbie on test domain :). So trying to figure out the concepts of Mockito
    – quartaela
    Jan 21, 2014 at 17:22

2 Answers 2

4

Spying is not needed, mocking is enough. As @David said, also mocking is not needed and not recommended for value object.

Using @Test(expected = Exception.class) has many drawbacks, test can pass when exception is thrown from not expected places. Test is not working but is visible as green.

I prefer BDD style testing with catch-exception.

Reasons for using catch-exceptions

(...) in comparison to the use of try/catch blocks.

  • The test is more concise and easier to read.
  • The test cannot be corrupted by a missing assertion. Assume you forgot to type fail() behind the method call that is expected to throw an exception.

(...) in comparison to test runner-specific mechanisms that catch and verify exceptions.

  • A single test can verify more than one thrown exception.
  • The test can verify the properties of the thrown exception after the exception is caught.
  • The test can specify by which method call the exception must be thrown.
  • The test does not depend on a specific test runner (JUnit4, TestNG).
import static com.googlecode.catchexception.CatchException.caughtException;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.*;

public class FooBarTest {

    FooBar sut = new FooBar(); // System Under Test

    @Test
    public void shouldThrowExceptionWhenListHasTooManyElements() {

        when(sut).getMethod(listWithSize(150));

        then(caughtException()).isInstanceOf(Exception.class);
    }

    private List<String> listWithSize(int size) {
        return new ArrayList<String>(Arrays.asList(new String[size]));
    }
}

Full working code for this test: https://gist.github.com/mariuszs/8543918


Not recommended solution with expected and mocking.

@RunWith(MockitoJUnitRunner.class)
public class FooBarTest {

    @Mock
    List<String> codes;

    FooBar fooBarInstance = new FooBar();

    @Test(expected = Exception.class)
    public void shouldThrowExceptionWhenListHasTooManyElements() throws Exception {

        when(codes.size()).thenReturn(150);

        fooBarInstance.getMethod(codes);

    }
}
15
  • hi MariuszS is there any important differences between using PowerMockRunner and MockitoJUnitRunner ?. and thanks a lot for your reply.
    – quartaela
    Jan 21, 2014 at 17:20
  • 1
    PowerMock is for testing finals and static, legacy code, while Mockito is for testing well designed code.
    – MariuszS
    Jan 21, 2014 at 17:31
  • Note that what OP wrote - doReturn(150).when(codes).size(); is fine too. It has the same effect as when(codes.size()).thenReturn(150); or given(codes.size()).willReturn(150);, and in my opinion is preferable, because it means that you don't have to learn a different syntax for stubbing void methods. Jan 21, 2014 at 18:20
  • 1
    In BDD style test keywords given/when/then looks better, whole test is more readable.
    – MariuszS
    Jan 21, 2014 at 19:05
  • 1
    Split test into two parts (classes) with different Runner.
    – MariuszS
    Jan 21, 2014 at 19:30
2

A list is a value object. It's not something we should mock. You can write this whole test without mocking anything, if you're prepared to build a list that has a size in excess of 100.

Also, I prefer to use JUnit's ExpectedException mechanism, because it lets you check which line of the test method threw the exception. This is better than passing an argument to the @Test annotation, which only lets you check that the exception was thrown somewhere within the method.

public class FooBarTest {
    @Rule 
    public ExpectedException exceptionRule = ExpectedException.none();
    private FooBar toTest = new FooBar();

    @Test
    public void getMethodThrowsException_whenListHasTooManyElements() {
        List<String> listWith101Elements = 
            new ArrayList<String>(Arrays.asList(new String[101]));

        exceptionRule.expect(Exception.class);
        toTest.getMethod(listWith101Elements);
    }
}
12
  • You are right, but without mock you need to use good name for list like listWith101Elements, with mock code is readable without any special naming. Also your listWith101Elements has fixed sized, if in getMethod we add or remove something from list then whole test will failure - this is bad side effect.
    – MariuszS
    Jan 21, 2014 at 19:07
  • Well I guess if I initiate a list with 101 objects and pass it, I won't face with any issues and the test will finish successfully. However, should I use Mocking and Spying whenever possible or if passing and testing behaviour of methods with real initialized objects is more easy then should I use the second way ? I mean is there any standart ?
    – quartaela
    Jan 21, 2014 at 19:32
  • Imagine situation when someone after some time add to getMethod this code: code.add("Foo");. This was needed for some new features. After this, test and only test throws Exception java.lang.UnsupportedOperationException, test will pass because we expect Exception but is not testing size() any more. This happens because you are testing fixed-size list backed by the specified array, this is not normal list.
    – MariuszS
    Jan 21, 2014 at 20:13
  • Absolutely. In "real life", you wouldn't just throw new Exception(), you'd throw new MySpecificFooBarException("Too many elements in list") or something similar. The beauty of the ExpectedException way of working is that you can check the class of the exception, you can check the message inside the exception, AND you can make sure the exception is thrown at the right place in the test (instead of in the set-up code for example). The old-style @Test(...) way of doing it can only check the class of the exception, which is why ExpectedException is much better. Jan 21, 2014 at 20:18
  • You are right, but it is better when test is less sensitive to changes made in production code. In this case test will fail.
    – MariuszS
    Jan 21, 2014 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.