You should be familiar with JUnit as it is what OpenMRS uses for unit testing. For my test, I used JUnit4. Please see The unit testing tutorial and The module unit testing tutorial for more information. It is assumed for the sake of this tutorial that you have read and understand those.
...
Code Block |
---|
WebConversation wc
@After
void cleanUp() {
wc = null
}
|
The above line is needed so that we can clean up after we finish testing. That should be the first method in your test class. Now let's write our first test method, I'm testing my AJAX Servlet which is responsible for validation/generation of the controller/view for the Groovy Forms Module /wiki/spaces/AR/pages/18513993.
I am only going to post one test method here, for the full class see this link. It should give you the overall gist. Assume for the sake of this test method that a static import exists for assertTrue and assertEquals (as that is the case in my test class.)
Code Block |
---|
@Test void testCheckSyntaxForModel() { wc = new WebConversation() def rq = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/groovyforms/createGroovyForm") rq.setParameter "clazz", "class MyForm { }" rq.setParameter "validate", "true" rq.setParameter "name", "My Form" rq.setParameter "version", "1.0" def response = wc.getResponse(rq) assertEquals "<result>true</result>", response.text // test when no class definition is given rq.setParameter "clazz", "" response = wc.getResource(rq) assertEquals "<result>false</result>", response.text // test invalid syntax rq.setParameter "clazz", "class MyForm { String }" wc = new WebConversation() response = wc.getResponse(rq) assertTrue "Should be true", response.text.indexOf("Exception") > 0 } |
...
Hop onto IRC; my nick is r0bby, and I'll be happy to answer any questions, my contact info is also available on my User page Robby O'Connor, feel free to contact me with questions.
...