• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How initialize request and response object in test class

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

i was written in vrifyLink();
public String verifyLink()
{
int empid = Integer.parseInt(request.getParameter("id"));
System.out.println(" empid " + empid);
employee = toCapDao.retriveFactsheet(empid);
String link = employee.getCapabilityBuilding();
}

And i want to test this method like public void testverifyLink(){} inside TestCapabiltyBuildingAction class.
I want to initialize the HttpServletRequest and HttpServletResponse object into this method; to initialize the
int empid = Integer.parseInt(request.getParameter("id")); in above nethod; but how i could achieve this.

Somebody tells he/she will be good appriciated.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I'd either (a) mock the request object, or (b) pass in the pre-parsed parameter.

I believe your verifyLink method is doing too much (well, actually, it doesn't do anything except set "employee" since nothing is done with the link). It does more than "verify a link", it also is responsible for getting values from the request. I'd much rather see something like:Getting stuff out of the request should be handled elsewhere, making the method that much easier to test.
 
Suraj Savaratkar
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But i was try to use
@Test
public void testverifyLink()
{
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
}

To this api which '.jar' is required and how to pass the request and response object over above method.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've missed most of my point. You don't pass the mocked objects to the method--you can't, because of the way you built the method. If you do it my way, you don't *need* to mock the objects anyway.

That aside, there are several implementations of MockHttpServletRequest, I have no idea which one you're referring to.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic