Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts

Friday, March 20, 2015

Browse H2 in-memory database in tests

If you sometimes want to look into H2 tables during the test, there is a problem, because by default in-memory H2 database is accessible only from the process where it was created. Simple way to browse the data looks like this:

Server.createWebServer().start()

It starts the web server on port 8082, so you can connect there, enter DB url, username and password (usually username and password are empty, you don’t need to protect your in-memory test database, do you?) and voila! You can browse the data.

Just remember to put some breakpoint in the test code, because after the test the process is finished, and so is the server. Another thing to remember is to set the breakpoint to stop only current thread, not all threads (like it is by default in Idea), because if you stop all of them, the server one is also stopped :)

Written with StackEdit.

Sunday, May 25, 2014

Validation of parameter passed to mock call in Spock

If you use Spock, sometimes you want to check mock method call. You can of course do it like this:
Problem here is that when the parameter passed to the method is wrong, you won't know what is wrong with it:
Too few invocations for:

1 * service.method({
            it.firstname == "Peter" && it.surname == "Stawicki"
        })   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * service.method(eu.vegasoft.spocktest.validateparam.Person@40f892a4)
Sometimes we want to validate also parameter passed to the mock method. We can do it like this:
Now when the passed parameter does not match, we can see exactly what was wrong with it:
Condition not satisfied:

p.firstname == "Peter"
| |         |
| Paweł     false
|           3 differences (40% similarity)
|           P(aw)e(ł)
|           P(et)e(r)
eu.vegasoft.spocktest.validateparam.Person@34b23d12