0

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.

Haider
  • 615
  • 1
  • 16
  • 38
  • You should write testcases on real data, not that faked datasources. – Roman C Aug 01 '14 at 16:24
  • @RomanC In my action class there is a function that gets the grid data i.e return the list of objects , and in that function i am using data- source to get the connection and query the db how can i provide jndi to InitContext lookup function – Haider Aug 01 '14 at 16:30
  • Then the issue is that your data gathering is too-tightly coupled to the JNDI resource. Abstract *how* you retrieve your data, not where. The action shouldn't have explicit implementations, by abstracting that you make it possible to test the action in isolation. – Dave Newton Aug 01 '14 at 21:10
  • @DaveNewton , then let say that i move that into another class e.g classA { function getData(){...}} and in action call that function in action class , now situation is same – Haider Aug 01 '14 at 21:30
  • No, the service class would be injected into the action. During unit testing you inject the implementation that returns known data. If you're using a JNDI datasource then you're doing *integration* testing, because you're testing multiple moving parts. – Dave Newton Aug 01 '14 at 22:24
  • @DaveNewton ok i understand , there is another question in my action page principleProxy is null during test. Do i have to set it by myself – Haider Aug 01 '14 at 23:52
  • @Haider You should run tests on both production and development, and test environment. The configuration is very different for each one, it's created at the time you build the system, at the same time you are build the tests. You can't build/deploy the product until you pass all tests. About what InitContext are you talking? If you turn off data sources the application stop working as well as tests fail. – Roman C Aug 02 '14 at 10:48
  • @RomanC InitialContext ctx = null; ctx = new InitialContext(); return ctx.lookup(strJndi); this statements are called during my test cases and i search in google and learnt that you can inject local datasource through spring i am going the right way ? and also in test i am using principleProxy.getRemoteUser() the principleProxy is always null in test , i tried request.setRemoteUser(userName); but it does do anything any idea what i can do here? – Haider Aug 02 '14 at 11:44
  • May be you need to provide the RemoteUser with initial context lookups on their JNDIs? – Roman C Aug 02 '14 at 12:04
  • @RomanC i used http://stackoverflow.com/a/5947016/1574921 but getting javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable – Haider Aug 02 '14 at 15:00
  • @Haider You have to tell it to the author of the post, also read comments below it. – Roman C Aug 02 '14 at 15:09
  • @RomanC in test cases i want to initialize some resources , and setUp is called multiple times is there any function available and @ BeforeClass is not working – Haider Aug 02 '14 at 21:00

1 Answers1

1

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.

nmc
  • 8,724
  • 5
  • 36
  • 68