You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Mark <el...@gmail.com> on 2005/12/13 19:42:34 UTC

unit testing servlets

I want to write some unit tests for a web site I have developed.  The front
page of my site requires a login.  Is is possible to programatically log in
using httpunit or cactus and then continue testing of the other servlets on
my site?

TIA

Re: unit testing servlets

Posted by Rakesh Patel <ra...@gmail.com>.
ok, here's my testcase that does a number of things:

1. I populate the db before each test and i use spring for this.
2. I also use log4j and so there's some configuration for that.
3. I found further refactirng was possible to reuse little bits of code 
here and there.

Any questions, let me know.

Rakesh

package com.acme.ifst.web.tests;

import net.sourceforge.jwebunit.WebTestCase;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;


public class ADTVWebTestCase extends WebTestCase {
    final static Logger _logger = Logger.getLogger(ADTVWebTestCase.class);
    private DriverManagerDataSource dataSource;
    private JdbcTemplate jt;

    public void testLoginLogoutSuccess() {
        login();
        assertTitleEquals("ADTV :: Main Menu");
        assertLinkPresent("logoutlink");
        logout();
    }

    public void testNavigationLinks() {
        login();

        // at main page
        // go to query page
        followLinkAndConfirmPage("querylink", "ADTV :: Query Screen");

        // go back to main page
        followLinkAndConfirmPage("mainpagelink", "ADTV :: Main Menu");

        //go to results page
        followLinkAndConfirmPage("resultslink", "ADTV :: Results Screen");

        // go back to main page
        followLinkAndConfirmPage("mainpagelink", "ADTV :: Main Menu");

        logout();
    }
   
    public void testQuerySubmission() {
        login();
       
        followLinkAndConfirmPage("querylink", "ADTV :: Query Screen");
       
        assertFormPresent("queryForm");
        setFormElement("startDate", "01-Jan-2004");
        setFormElement("endDate", "01-Aug-2004");
        submit();
       
        assertTitleEquals("ADTV :: Submitted");
       
        logout();
       
    }

    private void followLinkAndConfirmPage(String link, String pageName) {
        assertLinkPresent(link);
        clickLink(link);
        assertTitleEquals(pageName);
    }

    private void logout() {
        followLinkAndConfirmPage("logoutlink", "ADTV :: Logout");
    }

    private void login() {
        beginAt("/login.htm");
        assertFormPresent("login");
        assertFormElementPresent("username");
        assertFormElementPresent("password");
        setFormElement("username", "WebTester1");
        setFormElement("password", "Passw0rd");
        submit();
    }

    public void setUp() throws Exception {
        DOMConfigurator.configure(
            
"C:\\Projects\\ADTV\\src\\com\\acme\\ifst\\web\\tests\\log4j.xml");

        _logger.debug("running setUp()...");
        getTestContext().setBaseUrl("https://www.ADTV.acme.com/ADTV");

        dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl("jdbc:oracle:thin:@149.47.50.231:1521:ADDWD");
        dataSource.setUsername("ADTV_owner");
        dataSource.setPassword("ADTV_owner");

        jt = new JdbcTemplate(dataSource);
        jt.execute(
            "INSERT INTO T_SECUSER (USERNAME, PASSWORD, LOGINSTATUSTYPE 
) VALUES ('WebTester1','1900A91E289854A76EAF9D080DB0DEC4',1)");
        jt.execute(
            "INSERT INTO T_SECUSERATTRIBUTE (USERNAME, 
USERATTRIBUTETYPE, VALUE) VALUES ('WebTester1',1,'someone@somewhere.com')");
        jt.execute(
            "INSERT INTO T_SECUSERATTRIBUTE (USERNAME, 
USERATTRIBUTETYPE, VALUE) VALUES ('WebTester1',3,'MON')");
    }

    protected void tearDown() throws Exception {
        _logger.debug("tearDown()");

        jt.execute("DELETE FROM T_ADTVQUERY WHERE USER_ID='WebTester1'");
        jt.execute("DELETE FROM T_SECUSERATTRIBUTE WHERE 
USERNAME='WebTester1'");
        jt.execute("DELETE FROM T_SECUSER WHERE USERNAME='WebTester1'");
    }

    public static void setLoggerLevel(String loggerName, Level aLevel) {
        if (LogManager.exists(loggerName) != null) {
            Logger log = LogManager.getLogger(loggerName);
            log.setLevel(aLevel);
        }
    }
}


Mark wrote:

>I agree, post examples!
>
>Thank you.
>
>On 12/15/05, Julián García <jg...@unionsoluciones.com.co> wrote:
>  
>
>>Posting an example would be great!
>>Rakesh Patel wrote:
>>    
>>
>>>I can heartily recommend JWebUnit. Its built on top of HttpUnit i think
>>>but its a lot easier to work with.
>>>
>>>I can post an example if you would like to see it.
>>>
>>>Rakesh
>>>
>>>Jacob Kjome wrote:
>>>
>>>      
>>>
>>>>Quoting Mark <el...@gmail.com>:
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>>>I want to write some unit tests for a web site I have developed.  The
>>>>>front
>>>>>page of my site requires a login.  Is is possible to programatically
>>>>>log in
>>>>>using httpunit or cactus and then continue testing of the other
>>>>>servlets on
>>>>>my site?
>>>>>
>>>>>          
>>>>>
>>>>If it is BASIC Auth, then add the following to the URL you are
>>>>        
>>>>
>>connecting
>>    
>>
>>>>with...
>>>>
>>>>http://username:password@www.mysite.com/
>>>>
>>>>The username/password combo will be picked up by Basic Auth and used
>>>>        
>>>>
>>for
>>    
>>
>>>>authorization rather than prompting for input.  If you are using form
>>>>auth or
>>>>something else, I'm sure httpunit and/or cactus have ways to get you
>>>>logged in.
>>>>I just don't know, offhand, what those ways are.
>>>>
>>>>
>>>>Jake
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>>>TIA
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>
>>>>
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>>
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
>>    
>>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: unit testing servlets

Posted by Mark <el...@gmail.com>.
I agree, post examples!

Thank you.

On 12/15/05, Julián García <jg...@unionsoluciones.com.co> wrote:
>
> Posting an example would be great!
> Rakesh Patel wrote:
> > I can heartily recommend JWebUnit. Its built on top of HttpUnit i think
> > but its a lot easier to work with.
> >
> > I can post an example if you would like to see it.
> >
> > Rakesh
> >
> > Jacob Kjome wrote:
> >
> >> Quoting Mark <el...@gmail.com>:
> >>
> >>
> >>
> >>> I want to write some unit tests for a web site I have developed.  The
> >>> front
> >>> page of my site requires a login.  Is is possible to programatically
> >>> log in
> >>> using httpunit or cactus and then continue testing of the other
> >>> servlets on
> >>> my site?
> >>>
> >>
> >>
> >> If it is BASIC Auth, then add the following to the URL you are
> connecting
> >> with...
> >>
> >> http://username:password@www.mysite.com/
> >>
> >> The username/password combo will be picked up by Basic Auth and used
> for
> >> authorization rather than prompting for input.  If you are using form
> >> auth or
> >> something else, I'm sure httpunit and/or cactus have ways to get you
> >> logged in.
> >> I just don't know, offhand, what those ways are.
> >>
> >>
> >> Jake
> >>
> >>
> >>
> >>> TIA
> >>>
> >>>
> >>
> >>
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >>
> >>
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

Re: unit testing servlets

Posted by Julián García <jg...@unionsoluciones.com.co>.
Posting an example would be great!
Rakesh Patel wrote:
> I can heartily recommend JWebUnit. Its built on top of HttpUnit i think 
> but its a lot easier to work with.
> 
> I can post an example if you would like to see it.
> 
> Rakesh
> 
> Jacob Kjome wrote:
> 
>> Quoting Mark <el...@gmail.com>:
>>
>>  
>>
>>> I want to write some unit tests for a web site I have developed.  The 
>>> front
>>> page of my site requires a login.  Is is possible to programatically 
>>> log in
>>> using httpunit or cactus and then continue testing of the other 
>>> servlets on
>>> my site?
>>>   
>>
>>
>> If it is BASIC Auth, then add the following to the URL you are connecting
>> with...
>>
>> http://username:password@www.mysite.com/
>>
>> The username/password combo will be picked up by Basic Auth and used for
>> authorization rather than prompting for input.  If you are using form 
>> auth or
>> something else, I'm sure httpunit and/or cactus have ways to get you 
>> logged in.
>> I just don't know, offhand, what those ways are.
>>
>>
>> Jake
>>
>>  
>>
>>> TIA
>>>
>>>   
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
>>  
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: unit testing servlets

Posted by Rakesh Patel <ra...@gmail.com>.
I can heartily recommend JWebUnit. Its built on top of HttpUnit i think 
but its a lot easier to work with.

I can post an example if you would like to see it.

Rakesh

Jacob Kjome wrote:

>Quoting Mark <el...@gmail.com>:
>
>  
>
>>I want to write some unit tests for a web site I have developed.  The front
>>page of my site requires a login.  Is is possible to programatically log in
>>using httpunit or cactus and then continue testing of the other servlets on
>>my site?
>>    
>>
>
>If it is BASIC Auth, then add the following to the URL you are connecting
>with...
>
>http://username:password@www.mysite.com/
>
>The username/password combo will be picked up by Basic Auth and used for
>authorization rather than prompting for input.  If you are using form auth or
>something else, I'm sure httpunit and/or cactus have ways to get you logged in.
> I just don't know, offhand, what those ways are.
>
>
>Jake
>
>  
>
>>TIA
>>
>>    
>>
>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: unit testing servlets

Posted by Jacob Kjome <ho...@visi.com>.
Quoting Mark <el...@gmail.com>:

> I want to write some unit tests for a web site I have developed.  The front
> page of my site requires a login.  Is is possible to programatically log in
> using httpunit or cactus and then continue testing of the other servlets on
> my site?

If it is BASIC Auth, then add the following to the URL you are connecting
with...

http://username:password@www.mysite.com/

The username/password combo will be picked up by Basic Auth and used for
authorization rather than prompting for input.  If you are using form auth or
something else, I'm sure httpunit and/or cactus have ways to get you logged in.
 I just don't know, offhand, what those ways are.


Jake

>
> TIA
>




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org