#42 verify to handle null arguments

EasyMock_2.4
open
5
2012-10-05
2011-03-22
No

I would like verify() to silently accept a null pointer. It would remove a lot of boilerplate code in my unit tests.

Expected behavior:
verify(null) doesn't throw

Actual behavior:
verify(null) throws NullPointerException

Discussion

  • Henri Tremblay

    Henri Tremblay - 2011-03-22

    Can you give me a use-case? I'm quite surprised that a null mock can be passed to EasyMock since the mock comes from the test.

     
  • Anonymous

    Anonymous - 2011-03-23

    I'm trying to keep my tests as short and sharp as possible, so I moved mock-up validation to the tear-down method; @AfterMethod using TestNG. I write a test like thus:

    @AfterMethod
    public void verifyMockups()
    {
    EasyMock.verify(myProvider);
    }

    @Test
    public void shouldGetAccount()
    {
    givenAnAccount();
    givenAnObjectProvider();
    givenACache();

    Account account = myCache.getAccount(ACCOUNT_ID);
    
    Assert.assertEquals(account, myAccount);
    

    }

    If I add a test which doesn't use a provider, myProvider will be null, and the verification will fail, unless I add boilerplate code to check for null before validating.

    I would like to avoid the boilerplate code.

    /Roger

     

Log in to post a comment.