1

I have access to the Code base (checked out to my local machine from SVN). It is written using Java and Groovy using Grails framework (MVC architecture). I am a tester and as part of automating my tests, I want to write code that will make calls to the controllers and in return i can check the result in terms of looking at response or entries in data base. I basically want to skip the UI part.

How can I start? I probably cannot write my code inside the dev project (i am not allowed to i suppose). Do i need to create a separate framework for it? Or can I take all the jar files and include then in a project and write code on top of it?

The answer in this post is actually what I am looking for but for a Java application. Is there any API i can use?

Please let me know if you need additional information.

Community
  • 1
  • 1
Prabhat
  • 143
  • 2
  • 8
  • 4
    Why can't you use well documented and well integrated testing facilities available in Grails framework? – RaviH Jan 29 '14 at 12:27

1 Answers1

1

If the application does not provide Json, XML or similar APIs, you can use a test library like HtmlUnit within jUnit test methods.

A example from "Getting Started" section:

@Test
public void homePage_Firefox() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    webClient.closeAllWindows();
}

Note that HtmlUnit tries to work like a virtual browser (written 100% in Java), but it is a bit limited in executing Javascript, for example.

Then, use another library like jsoup or Jericho HTML Parese to inspect the code and get the values you want to check in the database.

In the other hand, if the application does provide methods to obtain the data, you can use Jersey Client API to make REST requests and get the values. It is very simple. Look at this example:

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:9998").path("resource");

Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");

MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
    .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
        MyJAXBBean.class);
utluiz
  • 289
  • 4
  • 22