【问题标题】:Test Servlet using JUnit and EasyMock Java使用 JUnit 和 EasyMock Java 测试 Servlet
【发布时间】:2012-05-01 19:16:03
【问题描述】:

我有一个 servlet,我想使用 EasyMock 对其进行测试。

我的 servlet 是这样的:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Population population = new Population();
    Elections elections = new Elections();
    ArrayList<Elector> electorsArl = new ArrayList<Elector>();
    ArrayList<Candidate> candidatesArl =  new ArrayList<Candidate>();

    String[] name = new String[4];
    String[] party = new String[4];
    String[] municipality = {"X", "Y", "Z", "A"};

    name[0] = request.getParameter("cand1");
    party[0] = request.getParameter("party1");
    name[1] = request.getParameter("cand2");
    party[1]= request.getParameter("party2");
    name[2] = request.getParameter("cand3");
    party[2]= request.getParameter("party3");
    name[3] = request.getParameter("cand4");
    party[3]= request.getParameter("party4");

    population.reducePopulation(candidatesArl);

    String[] politicalParty = {candidatesArl.get(0).getPoliticalParty(), candidatesArl.get(1).getPoliticalParty(), candidatesArl.get(2).getPoliticalParty(), candidatesArl.get(3).getPoliticalParty()};

    population.genPopulation(population.getMtyPopulation(), "X", electorsArl);
    population.genPopulation(population.getGpePopulation(), "Y", electorsArl);
    population.genPopulation(population.getSpPopulation(), "Z", electorsArl);
    population.genPopulation(population.getSnPopulation(), "A", electorsArl);

    elections.vote(electorsArl, candidatesArl, politicalParty);
    request.setAttribute("candidate", candidatesArl);

    int totalPopulation = electorsArl.size() + 4;
    int populationVoted = 0;
    for (int i = 0; i < candidatesArl.size(); i++){
        populationVoted = populationVoted + candidatesArl.get(i).getAllVotes();
    }
    Integer totalPercentage = (Integer) (populationVoted * 100) / totalPopulation;

    Integer mtyPercentage = (Integer) ((candidatesArl.get(0).getMtyVotes() + candidatesArl.get(1).getMtyVotes() + candidatesArl.get(2).getMtyVotes() + candidatesArl.get(3).getMtyVotes()) * 100) / 90;
    request.setAttribute("mtyPercentage", mtyPercentage);
    Integer spPercentage = (Integer) ((candidatesArl.get(0).getSpVotes() + candidatesArl.get(1).getSpVotes() + candidatesArl.get(2).getSpVotes() + candidatesArl.get(3).getSpVotes()) * 100) / 90;
    request.setAttribute("spPercentage", spPercentage);
    Integer snPercentage = (Integer) ((candidatesArl.get(0).getSnVotes() + candidatesArl.get(1).getSnVotes() + candidatesArl.get(2).getSnVotes() + candidatesArl.get(3).getSnVotes()) * 100) / 90;
    request.setAttribute("snPercentage", snPercentage);
    Integer gpePercentage = (Integer) ((candidatesArl.get(0).getGpeVotes() + candidatesArl.get(1).getGpeVotes() + candidatesArl.get(2).getGpeVotes() + candidatesArl.get(3).getGpeVotes()) * 100) / 90;
    request.setAttribute("gpePercentage", gpePercentage);
    request.setAttribute("totalPercentage", totalPercentage);

    request.setAttribute("winner", elections.getWinnerCandidate(candidatesArl.get(0), candidatesArl.get(1), candidatesArl.get(2), candidatesArl.get(3)));
    try {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        ElectionsDao electionsDao = (ElectionsDao) context.getBean("electionsDao");
        electionsDao.insertResults(candidatesArl, mtyPercentage, spPercentage, snPercentage, gpePercentage, totalPercentage);
        RequestDispatcher view = request.getRequestDispatcher(FORWARD_PAGE);
        view.forward(request, response); 
    } catch (RuntimeException re){
        logger.error("Runtime exception ocurred in ElectionsServlet.doPost", re);
        response.sendRedirect("error.html");
    }
}

我对此的看法是,我只需要测试必须处理请求和响应的所有内容,因为所有其他方法都在其他类中进行了测试。我想知道是否有人可以帮助我。

到目前为止我有这个:

public class ElectionsServletTest extends TestCase {

private IMocksControl mocks;
private ElectionsServlet servlet;

@BeforeClass
public void setUpBeforeClass() {
    mocks = (IMocksControl) createStrictControl();
    servlet = new ElectionsServlet();
}

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

@Test
public void testDoPostHttpServletRequestHttpServletResponse() throws IOException, ServletException{
    HttpServletRequest request = mocks.createMock(HttpServletRequest.class);
    HttpServletResponse response = mocks.createMock(HttpServletResponse.class);

    ArrayList<Candidate> candidatesArl =  new ArrayList<Candidate>();

    expect(request.getParameter("cand1")).andReturn("abc");
    expect(request.getParameter("party1")).andReturn("abc");
    expect(request.getParameter("cand2")).andReturn("bcd");
    expect(request.getParameter("party2")).andReturn("bcd");
    expect(request.getParameter("cand3")).andReturn("cde");
    expect(request.getParameter("party3")).andReturn("cde");
    expect(request.getParameter("cand4")).andReturn("def");
    expect(request.getParameter("party4")).andReturn("def");

    candidatesArl.add(new Candidate("abc", 35, "Monterrey", "abc"));
    candidatesArl.add(new Candidate("bcd", 35, "Monterrey", "bcd"));
    candidatesArl.add(new Candidate("cde", 35, "Monterrey", "cde"));
    candidatesArl.add(new Candidate("def", 35, "Monterrey", "def"));

    request.setAttribute("candidate", eq(isA(ArrayList.class)));

    request.setAttribute("mtyPercentage", eq(isA(Integer.class)));
    request.setAttribute("spPercentage", eq(isA(Integer.class)));
    request.setAttribute("snPercentage", eq(isA(Integer.class)));
    request.setAttribute("gpePercentage", eq(isA(Integer.class)));
    request.setAttribute("totalPercentage", eq(isA(Integer.class)));

    request.setAttribute("winner", "abc");

    ElectionsDao electionsDao = mocks.createMock(ElectionsDao.class);
    electionsDao.insertResults(candidatesArl, 45, 45, 45, 45, 45);

    expect(request.getRequestDispatcher("result.jsp")).andReturn(createMock(RequestDispatcher.class));
    mocks.replay();
    servlet.doPost(request, response);
    mocks.verify(); 
}

}

【问题讨论】:

  • 你是对的。您需要什么样的帮助?
  • 我需要对此进行测试...我一直在尝试使用 EasyMock 进行测试,但我真的不知道如何使用它。
  • 我已经编辑了我的帖子,让你看看我是如何尝试这样做的

标签: java testing servlets junit easymock


【解决方案1】:

据我所知,您使用这种方法做了很多事情。 因此,很难对其进行测试。 您应该在单独的方法和/或类中提取一些逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多