Creating database related JUNIT test cases for Struts2

622 Views Asked by At

I am using struts2-junit-plugin for writing test cases for struts2 web application my issue is that in my action classes there are some database related queries which uses data source(jndi) how can i simulate this in my test case.

EDIT

In this test i am setting the remote user.

public void testexecute()
{   
    try 
    {   
        ActionProxy proxy = getActionProxy("/index");

        IndexAction action = (IndexAction) proxy.getAction();

        request.setRemoteUser("Haider");


        assertTrue(action.execute().equals(ActionSupport.SUCCESS));

        assertTrue(true);
    }
    catch(Exception ex)
    {
        assertTrue(false);
    }       
}

and in IndexAction (implements PrincipalAware) I have this

 public String execute()
 {  
    try
    {   
        if(principleProxy != null)
        {
            userModel = new UserModel();
            userModel.setUserName(principleProxy.getRemoteUser());              
        }
        else
        {
            return ERROR;
        }       
  ................................
  .................................

 }

in index ation principleProxy is null when i run the test.

1

There are 1 best solutions below

0
On

You can look into using Dbunit

DbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that, among other things, puts your database into a known state between test runs. This is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to fail or exacerbate the damage.

DbUnit has the ability to export and import your database data to and from XML datasets. Since version 2.0, DbUnit can also work with very large datasets when used in streaming mode. DbUnit can also help you to verify that your database data match an expected set of values.