Link to home
Start Free TrialLog in
Avatar of k41d3n
k41d3n

asked on

junit, httpunit and logging

Alright, here is what I am trying to do.

I am using httpunit to test a website.

I also have a database to record test results.

I am very new to unit testing.

When I am running a test, how do I get junit to record a failure, or even better, log a failure to my database? Here is one of my tests:

/*
 * Created on May 5, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.apc.qa.ate.test.http;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import com.apc.qa.ate.functions.HttpFunctions;
import com.apc.qa.ate.test.LoginPasswordTestCase;
import com.apc.qa.ate.util.FieldConstants;
import com.apc.qa.ate.util.LinkConstants;
import com.apc.qa.ate.util.Util;
import com.apc.qa.ate.util.ValueConstants;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebImage;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;

/**
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 *
 * @author marcadam
 **/
public class DeviceIdentification extends LoginPasswordTestCase {
   
    public DeviceIdentification(String name, String _uname, String _pword) {
        super(name, _uname, _pword);
    }
 
    /**
     * Logger for this class
     */
    private static final Logger atelog = Logger.getLogger(HttpFunctions.class);
   

    public void testDeviceInfo()throws Exception {
        try {
           
            //New Web Conversation
            WebConversation wc = new WebConversation();
           
            //Web Response
            WebResponse resp = wc.getResponse(Util.getLoginUrl());

            //Get the Login Form
            WebForm form = resp.getForms()[0];
           
            //Set the form username and password
            form.setParameter("login_username", uname);
            form.setParameter("login_password", pword);
           
            //Submit the form
            form.submit();
           
            //Now check to see if you can access the main URL
            resp = wc.getResponse(Util.getMainIndex());
           
            //Assert that the login worked
            assertTrue("Login not accepted",
                    resp.getText().indexOf("Updated:") != -1);
           
            //Log the login only if you are logged in
            atelog.info("Logged in properly");
           
            WebLink admin = resp.getLinkWith(LinkConstants.LINK_ADMIN);
            assertNotNull("Finding Admin Link", admin);
            atelog.debug("Found Admin Link");
            resp = admin.click();
            WebLink general = resp.getLinkWith(LinkConstants.LINK_GEN);
            assertNotNull("Finding General Link", general);
            atelog.debug("Found General Link");
            resp = general.click();
            WebLink iden = resp.getLinkWith(LinkConstants.LINK_IDEN);
            assertNotNull("Finding Identification Link", iden);
            atelog.debug("Found Identification Link");
            resp = iden.click();
           
            WebForm ident = resp.getForms()[0];
            assertNotNull("Finding form", ident);
            atelog.debug("Found Web Form");
            ident.setParameter(FieldConstants.FIELD_SYSIDEN, ValueConstants.VALUE_SYSIDEN);
            atelog.info("Setting System Name: " + ValueConstants.VALUE_SYSIDEN);
            ident.setParameter(FieldConstants.FIELD_SYSCONT, ValueConstants.VALUE_SYSCONT);
            atelog.info("Setting System Contact: " + ValueConstants.VALUE_SYSCONT);
            ident.setParameter(FieldConstants.FIELD_SYSLOC, ValueConstants.VALUE_SYSLOC);
            atelog.info("Setting System Location: " + ValueConstants.VALUE_SYSLOC);
            ident.submit();
           
            resp = wc.getResponse(Util.getMainIndex());
           
            admin = resp.getLinkWith(LinkConstants.LINK_ADMIN);
            assertNotNull("Finding Admin Link", admin);
            atelog.debug("Found Admin Link");
            resp = admin.click();
            general = resp.getLinkWith(LinkConstants.LINK_GEN);
            assertNotNull("Finding General Link", general);
            atelog.debug("Found General Link");
            resp = general.click();
            iden = resp.getLinkWith(LinkConstants.LINK_IDEN);
            assertNotNull("Finding Identification Link", iden);
            atelog.debug("Found Identification Link");
            resp = iden.click();
           
            ident = resp.getForms()[0];
            assertEquals(ValueConstants.VALUE_SYSIDEN, ident.getParameterValue(FieldConstants.FIELD_SYSIDEN));
            assertEquals(ValueConstants.VALUE_SYSCONT, ident.getParameterValue(FieldConstants.FIELD_SYSCONT));
            assertEquals(ValueConstants.VALUE_SYSLOC, ident.getParameterValue(FieldConstants.FIELD_SYSLOC));
            atelog.info("System Name Set: " + ValueConstants.VALUE_SYSIDEN);
            atelog.info("System Contact Set: " + ValueConstants.VALUE_SYSCONT);
            atelog.info("System Location Set: " + ValueConstants.VALUE_SYSLOC);
           
            //Find the logout image
            WebImage image = resp.getImageWithSource("images/exit.gif");
            assertNotNull("Find Logout Image", image);
            atelog.debug("Found Logout Image");
           
            //Find the logout link associated with the image
            WebLink findLogoutLink = image.getLink();
            assertNotNull("Find Logout Link", findLogoutLink);
            atelog.debug("Found Logout Link");
           
            //click the link
            resp = findLogoutLink.click();
           
            assertNotNull("Logout Page", resp);
            assertTrue("Did not Log out", resp.getText().indexOf(
            "You are now logged off.") != -1);
            atelog.info("Successfully logged out");
        } catch (MalformedURLException e) {
            atelog.error(e);
            e.printStackTrace();
        } catch (IOException e) {
            atelog.error(e);
            e.printStackTrace();
        } catch (SAXException e) {
            atelog.error(e);
            e.printStackTrace();
        }
    }
}

Right now this test passes, but let's say it were to fail, and it has when I coded it. junit just threw an excepton and quit.

If I put 30 tests in a test suite, and one of them fails, I want to log it, and move on to the next test. Make sense?

There has to be a way to mark a test as failed and continue on.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of k41d3n
k41d3n

ASKER

Absolutely correct.

After mucking with junit some more I discovered all my assumptions in this question were incorrect.

Thank you.
NP. :-)