Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:40:39+00:00 2026-06-17T16:40:39+00:00

I want to test my servlet with http://www.easymock.org/ How do I write the Unit

  • 0

I want to test my servlet with http://www.easymock.org/

How do I write the Unit Testing Code?

I update my code with your responce.

I just used Google and made this code now.

Here is my Servlet:

 package com.i4ware.plugin.timesheet;

 import java.io.IOException;

 import com.atlassian.jira.issue.Issue;
 import com.atlassian.jira.issue.IssueManager;
 import com.atlassian.jira.project.Project;
 import com.atlassian.jira.project.ProjectManager;
 import org.ofbiz.core.entity.DelegatorInterface;
 import org.ofbiz.core.entity.EntityExpr;
 import org.ofbiz.core.entity.EntityOperator;
 import org.ofbiz.core.entity.GenericEntityException;
 import org.ofbiz.core.entity.GenericValue;
 import org.ofbiz.core.util.UtilMisc;
 import org.apache.commons.lang.StringEscapeUtils;
 import com.atlassian.crowd.embedded.api.User;
 import com.atlassian.jira.security.JiraAuthenticationContext;
 import com.atlassian.jira.web.bean.PagerFilter;
 import com.atlassian.jira.issue.search.SearchResults;
 import com.atlassian.jira.bc.issue.search.SearchService;
 import com.atlassian.jira.issue.search.SearchException;

 import com.atlassian.jira.issue.worklog.Worklog;
 import com.atlassian.jira.issue.worklog.WorklogManager;
 import com.atlassian.jira.issue.worklog.WorklogImpl;

 import com.atlassian.query.Query;
 import com.atlassian.jira.jql.builder.JqlQueryBuilder;
 import com.atlassian.query.order.SortOrder;

 import com.atlassian.jira.issue.status.Status; 

 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 import com.atlassian.jira.util.json.JSONObject;
 import com.atlassian.jira.util.json.JSONException;
 import com.atlassian.jira.util.json.JSONArray;

 import java.io.UnsupportedEncodingException;
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Collections;
import java.lang.Long;
import java.util.Collection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.Class;
import java.util.Enumeration;

import org.apache.log4j.Category;

import com.atlassian.upm.api.license.entity.PluginLicense;
import com.atlassian.upm.license.storage.lib.PluginLicenseStoragePluginUnresolvedException;
import com.atlassian.upm.license.storage.lib.ThirdPartyPluginLicenseStorageManager;

import com.atlassian.plugin.webresource.WebResourceManager;
 import com.atlassian.templaterenderer.TemplateRenderer;

import java.text.ParseException;
import java.text.ParsePosition;

 public class UserIsLogedInServlet extends HttpServlet
 {
private static final Category log = Category.getInstance(UserIsLogedInServlet.class);
/** value is made for JSON {"success":true} or {"success":false}. */
private Boolean value;    

private String json;
private String msg;
private final ThirdPartyPluginLicenseStorageManager licenseManager;
private WebResourceManager webResourceManager;
private final TemplateRenderer renderer;

private JiraAuthenticationContext authenticationContext;    

public UserIsLogedInServlet(ThirdPartyPluginLicenseStorageManager licenseManager,
        WebResourceManager webResourceManager,
        TemplateRenderer renderer,
        JiraAuthenticationContext authenticationContext)
{
    this.licenseManager = licenseManager;
    this.webResourceManager = webResourceManager;
    this.renderer = renderer;
    this.authenticationContext = authenticationContext;
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    resp.setContentType("application/json");        

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{       

    User targetUser = authenticationContext.getLoggedInUser();

    String user = "";

    if (targetUser==null) {

        user = "anonymous";
        value = Boolean.valueOf(!"false"
            .equalsIgnoreCase((String) "false"));
        msg = "You're not loged in.";
    } else {

        user = targetUser.getName();
        value = Boolean.valueOf(!"false"
            .equalsIgnoreCase((String) "true"));
        msg = "You're loged in.";
    }        

    try {

    json = new JSONObject()      
    .put("msg", msg)
    .put("success", value)
    .toString();

    } catch (JSONException err) {
        err.printStackTrace();
        System.out.println("Got an JSONException: " + err.getCause());
    }

    resp.setContentType("application/json");        
    resp.getWriter().write(json);
    resp.getWriter().close();

}
}

Here is code:

package com.i4ware.plugin.timesheet;

import junit.framework.*;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.BeforeClass;
import org.junit.After;
import java.io.*;
import java.security.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import static org.easymock.EasyMock.*;
import org.easymock.IMocksControl;
import com.atlassian.upm.api.license.entity.PluginLicense;
import com.atlassian.upm.license.storage.lib.PluginLicenseStoragePluginUnresolvedException;
import com.atlassian.upm.license.storage.lib.ThirdPartyPluginLicenseStorageManager;
import com.atlassian.plugin.webresource.WebResourceManager;
import com.atlassian.templaterenderer.TemplateRenderer;
import com.atlassian.jira.security.JiraAuthenticationContext;

import com.i4ware.plugin.timesheet.UserIsLogedInServlet;

public class UserIsLogedInServletTest extends TestCase {    

private ThirdPartyPluginLicenseStorageManager licenseManager;
private WebResourceManager webResourceManager;
private TemplateRenderer renderer;  
private JiraAuthenticationContext authenticationContext;

private IMocksControl mocks;
private UserIsLogedInServlet servlet;

@BeforeClass
public void setUpBeforeClass() {
    mocks = (IMocksControl) createStrictControl();
    servlet = new UserIsLogedInServlet(licenseManager,webResourceManager,renderer,authenticationContext);
}

@After
public void tearDown() {
    mocks.reset();
}

@Test
public void testGet()throws ServletException, IOException {
    HttpServletRequest request = mocks.createMock(HttpServletRequest.class);
    HttpServletResponse response = mocks.createMock(HttpServletResponse.class);
expect(request.getRequestDispatcher("/plugins/servlet/timesheet/userislogedin")).andReturn(createMock(RequestDispatcher.class));
    replay(request, response);
    servlet.doGet(request, response);
    verify(request, response);
}

@Test
public void testPost() throws ServletException, IOException {
    HttpServletRequest request = mocks.createMock(HttpServletRequest.class);
    HttpServletResponse response = mocks.createMock(HttpServletResponse.class);
expect(request.getRequestDispatcher("/plugins/servlet/timesheet/userislogedin")).andReturn(createMock(RequestDispatcher.class));
    replay(request, response);
    servlet.doPost(request, response);
    verify(request, response);
}

}

I get this error:

Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.046 sec <<< FAILURE!
testPost(com.i4ware.plugin.timesheet.UserIsLogedInServletTest)  Time elapsed: 0.01 sec  <<< ERROR!
 java.lang.NullPointerException
at  com.i4ware.plugin.timesheet.UserIsLogedInServletTest.testPost(UserIsLogedInServletTest.java:67)

testGet(com.i4ware.plugin.timesheet.UserIsLogedInServletTest)  Time elapsed: 0 sec  <<< ERROR!
java.lang.NullPointerException
at com.i4ware.plugin.timesheet.UserIsLogedInServletTest.testGet(UserIsLogedInServletTest.java:58)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:40:40+00:00Added an answer on June 17, 2026 at 4:40 pm

    I am more familiar with Mockito, but I believe they are still similar. In fact I think Mockito was branched from EasyMock a few years back.

    There is no cookbook approach to unit testing, but this is the basic approach I tend to take:

    1) Create a real instance of your servlet class (i.e. new MyServlet())

    2) Create a mock HttpRequest using EasyMock

    2a) Mock the desired behavior of the request to simulate a real HTTP request. For example, this may mean you simulate the presence of request parameters or headers.

    3) Create a mock HttpResponse using EasyMock

    4) call the doGet() method of your servlet passing it both the mock request and response.

    5) For verification, inspect the mock HttpResponse. Verify that: (a) the expected methods have been called on the object, (b) The expected data has been passed to the object.

    I know this is very high-level, but I am just outlining the approach. I assume you know how to accomplish the mocking/verification behaviors using EasyMock.

    Hope this is helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I initially just had a Java Servlet that I needed to unit test. I
I want to write a simple P2P test app which uses HTTP as underlying
I want to test the following code: private bool TestException(Exception ex) { if ((Marshal.GetHRForException(ex)
I want to build a very simple servlet that will read the http posted
I want to dump some requests (of type javax.servlet.http.HttpServletRequest ) into a file and
I have written servlet.I want to test the functionality of my servlet class ,so
I am trying to :gen-class a Servlet This is my code: (ns test.test (:import
I want test a FIX gateway for our company and was wondering if anything
I want to test the login() action in my UsersController.php <?php class UsersController extends
I want to test it on a tablet because it is more natural to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.