Balki, Thanks for the details. It is what I understood. Let's solve it by asking you a few questions ?
1/ If I understand correctly, in the second test, you only need the cookie in order to access data in the HTTP Session ? Then the first lines of your testValidate() must be accessing the HTTP Session to put in it the needed values, so that auth.login() has what it needs, in the state it needs it. Here is a very simple way of thinking about it : For each method you want to test (e.g. auth.login()), think about all the "environment" setup that is needed for the method to perform the way you would like. This can involve : - setting HTTP parameters, HTTP cookies, HTTP Headers - setting values in HttpSession, ServletContext, ServletConfig - constructing java objects that need to be passed to your method - etc In your case, the second test does not seem to really need a cookie (Remember you're unit testing the method, not the way to get to it !). What it seems it needs is a given state of the HttpSession. Thus, you simply need to set it up before executing the test. I hope this is more clear ;-) Thanks -Vincent > -----Original Message----- > From: BALA KRISHNA [mailto:[EMAIL PROTECTED]] > Sent: 23 February 2002 00:59 > To: [EMAIL PROTECTED] > Subject: RE: Concatenating two cactus tests? > > Hi Vincent, > > > >In your test for testB, you need to add the cookie to > >the request, in > >beginXXX(). It is all about unit testing, i.e. about > >testing in > >isolation. You know the cookie name and you know > >values with which you > >wan to test. Thus, you can set the cookie in beginXXX > > >Do you have a use case in mind where this would not > >work ? > > An example wouldbe what I have given in my previous > email. Let me explain better this time by filling the > skeleton code of yours. > > beginTestLogin(...) > { > // set what is needed WRT the HTTP request > ... > req.addParameter("user", "test"); > req.addParameter("pwd", "test123"); > } > > testTestLogin() > { > // set what is need WRT container objects > ... > Authenticator auth = new Authenticator(); > // Execute the test > // This method will read user and pwd parameters, > // creates a custom sessionid and stores it as a > // cookie in the response. It also stores it in the > // database. > > auth.login(request, response); > ... > // Assert server side objects > ... > } > > endTestLogin(...) > { > // Assert HTTP related response values > ... > String sid = response.getCookie().getValue(); > // When I go to another page on the site, this > // cookie is required to validate the session and > // display the information. Authenticator class > // therefore has another API called validate that > // needs this cookie obtained in the response here. > // Therefore, I store it as a system property as I > // dont know other way of accessing this information > > // in next test. > > System.setProperty("sid", sid); > } > > // Now the test that I am interested in - validate > > beginTestValidate (...) > { > // I need to set a valid session id obtained on > // logging in to the application in the cookie of > // this request. Therefore, I read that cookie from > // previous test. If I dont pass a valid session id, > // I cannot test this method because this method > // returns information only if it can find the > // session id passed in the cookie to already exist > // in the database (inserted during login). > > String sid = System.getProperty("sid"); > request.addCookie("sid", sid); > } > > testTestValidate() > { > Authenticator auth = new Authenticator(); > // Execute the test > // This method will read sid cookie, > // and returns true if validated. > > Boolean sidExists = auth.login(request, response); > > // I want to create a test where I sid already > exists > // so that following assert will not fail. > assertTrue("Could not validate", sidExists); > > } > > Now, if I dont save the result from a previous test or > use Method 2 where I use HttpUnit in beginTestValidate > to get a valid session id, how can I get > testTestValidate to work? > > beginXXX using Method 2: (I havnt tried this though) > > beginTestValidate (...) > { > > WebConversation conversation = new > WebConversation(); > PostMethodWebRequest request2 = new > PostMethodWebRequest("http://localhost:8080/test/login.jsp"); > request2.setParameter( "user", "test" ); > request2.setParameter( "pwd", "test123" ); > WebResponse response2 = conversation.getResponse( > request2 ); > String sid = response2.getNewCookieValue("sid"); > request.addCookie("sid",sid); > > } > > Thanks, > balki > > > __________________________________________________ > Do You Yahoo!? > Yahoo! Sports - Coverage of the 2002 Olympic Games > http://sports.yahoo.com > > -- > To unsubscribe, e-mail: <mailto:cactus-user- > [EMAIL PROTECTED]> > For additional commands, e-mail: <mailto:cactus-user- > [EMAIL PROTECTED]> > -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
