Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Mocking Question

Options
  • 30-11-2012 3:46pm
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    I have a method I'm unit testing in Java. The method contains another method. I want the second method to throw an exception to see how the outer method reacts. Do any of you know how to mock a call to the inner method to throw an exception.

    The issue here is the Mockito does not allow me to mock the inner method because it has a void return type.
    @Test(expected = MyException.class)
        public void testDo() throws Exception {
            // Given
            final Long id = 1L;
    
            given(myObj.doAnother(id)).thenThrow(MyException.class);
            classUnderTest.do(id);
        }
    
    
        public void do() throws Exception {
    
            myObj.doAnother(id)).thenThrow(MyException.class);
        }
    
        public void doAnother() throws Exception {
    
            //do whatever
        }
    


Comments

  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    You cant/shouldnt mock methods of the class you are testing, that kinda defeats the point of testing that class...
    What would cause the second method to throw an exception in real life?
    Im guessing either it interacts with some other object/system/resource that could throw it, or perhaps based on the value of id?
    So, if one of the above is correct you want to have a test where you pass an invalid "id" that will cause an exception to be thrown OR you want to mock the other resource/object/system and set it up to throw the exception in your test.

    make sense?
    I have a method I'm unit testing in Java. The method contains another method. I want the second method to throw an exception to see how the outer method reacts. Do any of you know how to mock a call to the inner method to throw an exception.

    The issue here is the Mockito does not allow me to mock the inner method because it has a void return type.
    @Test(expected = MyException.class)
        public void testDo() throws Exception {
            // Given
            final Long id = 1L;
    
            given(myObj.doAnother(id)).thenThrow(MyException.class);
            classUnderTest.do(id);
        }
    
    
        public void do() throws Exception {
    
            myObj.doAnother(id)).thenThrow(MyException.class);
        }
    
        public void doAnother() throws Exception {
    
            //do whatever
        }
    


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    GreeBo wrote: »
    You cant/shouldnt mock methods of the class you are testing, that kinda defeats the point of testing that class...
    What would cause the second method to throw an exception in real life?
    Im guessing either it interacts with some other object/system/resource that could throw it, or perhaps based on the value of id?
    So, if one of the above is correct you want to have a test where you pass an invalid "id" that will cause an exception to be thrown OR you want to mock the other resource/object/system and set it up to throw the exception in your test.

    make sense?

    Thanks for you reply I'll elaborate a little more.

    I have method that updates a DB record. This method has been throwing an Optimistic Locking exception. I am surrounding this method call in a try/catch/retry paradigm. I want my test to prove that, even though the method threw the exception, the database updating method was still call (upon retry).


  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    Ok, so what you need to do is abstract away the DBConnection from the method using/calling it.
    That way you can inject a mocked connection object and using the Mockito
    when(connection.doUpdate(instance I want to fail with)).thenThrowException(my exception) syntax you can get it to throw an exception back to your code under test.

    You can then assert that the method to make the DB update is called twice.


Advertisement