ssacher-tgm / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

How do i verify either-or methods? #164

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I am unit testing servlets and servlet filters.

I want to verify that *either* request.getSession() or
request.getSession(true) was called at least once. It does not matter which
was called or if both was called multiple times, as long as one of them was
called.

I also would like to be able verify that one of them was called either
before or after request.getSession(false) was called.

Can i do this with the current API?

Original issue reported on code.google.com by krei...@linuxgods.com on 25 Jan 2010 at 6:51

GoogleCodeExporter commented 8 years ago
Hi,

Current API does not support it explicitly. However, there is a good reason for 
that:
usually you should know exactly what happens in your code. After all, you 
control how
your system under test is set up. It seems that your case indicates a code 
smell.
Don't get fired up - I might be wrong :)

Let's say that you really have a legitimate use case for such feature. You 
still can
somehow work around:

1. You can develop a handy method that catches AssertionError and fail only if 
there
are 2 AssertionErrors. All mockito verification errors inherit from 
AssertionError.

2. You can stub both methods smartly with fancy Answer and later check what's 
up...

Original comment by szcze...@gmail.com on 25 Jan 2010 at 7:08

GoogleCodeExporter commented 8 years ago
The reason i wanted this is that i have 

    private void verifyExistingSessionRequested() {
        verify(request, never()).getSession();
        verify(request, never()).getSession(true);
        verify(request).getSession(false);
    }

to verify that getSession(false) was called, and not one of the others. I use 
this
multiple times in a test for different methods.

I wanted to create a corresponding method that verifies that either 
getSession(true)
or getSession() was called, to be used multiple times for different methods, 
some of
which might use getSession() and some of which might use getSession(true).

So in short i wanted to create a reusable verification method. And don't worry, 
i
won't get fired up. :)

Original comment by krei...@linuxgods.com on 25 Jan 2010 at 7:33

GoogleCodeExporter commented 8 years ago
How about this? :)

    private void verifySessionCreationRequested() {
        try {
            verify(request, never()).getSession();
            verify(request, never()).getSession(true);
            throw new WantedButNotInvoked("getSession() or getSession(true)");
        } catch (NeverWantedButInvoked ignored) {}
    }

Original comment by krei...@linuxgods.com on 25 Jan 2010 at 7:40

GoogleCodeExporter commented 8 years ago
good stuff ;)

Original comment by szcze...@gmail.com on 25 Jan 2010 at 8:39

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 14 Feb 2010 at 12:33