I started a new Grails project recently, and I found Canoo’s WebTest extremely easy to get working. I was even able to get Cobertura code coverage integrated to my WebTests. I found there where several error conditions that I did not test in my code. So I really wanted to simulate error POST method invocations with WebTest.
First I wanted to find a way to add arbitrary POST parameters. I accomplished this by creating the invocation like that would POST an ID of 99 when there is no record 99 in the database

    void testUpdateItemNotFound() {
        def post = "id=99&version=123"
        invoke( url: "http://localhost:8080/divr/contextConfig/update",
                method: 'POST',
                content: "${post}")

        verifyText(text: 'ContextConfig not found with id 99')
    }

This worked great

What I found is what Cobertura was able to measure my code coverage on my WebTests, so I was able to identify what error scenarios I did not cover in a test

Conclusion

Do not settle testing just the happy path in your application. I feel there is always more testing that can be done.

Mick Knutson

Java, JavaEE, J2EE, WebLogic, WebSphere, JBoss, Tomcat, Oracle, Spring, Maven, Architecture, Design, Mentoring, Instructor and Agile Consulting. http://www.baselogic.com/blog/resume

View all posts

Java / JavaEE / Spring Boot Channel

BLiNC Supporters

BLiNC Adsense

Archives

Newsletter