简体   繁体   English

使用JUnit和EasyMock Java测试Servlet

[英]Test Servlet using JUnit and EasyMock Java

I have a servlet and I would like to test it using EasyMock. 我有一个servlet,我想使用EasyMock对其进行测试。

My servlet is something like this: 我的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");
    }
}

What I think about this is that I just have to test everything that has to deal with the request and response because all the other methods are tested in other classes. 我对此的想法是,我只需要测试所有处理请求和响应的内容,因为所有其他方法都在其他类中进行了测试。 I was wondering if anyone could help me. 我想知道是否有人可以帮助我。

I have this so far: 到目前为止,我有:

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(); 
}

} }

From what I can see you are doing an awful lot of things in that method. 从我可以看出,您正在使用该方法做很多事情。 Thus it's going to be hard to test it. 因此,将很难对其进行测试。 You should extract some logic in separate methods and/or classes. 您应该在单独的方法和/或类中提取一些逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM