2012-05-01 11 views
2

サーブレットがあり、EasyMockを使ってテストしたいと思います。JUnitとEasyMock Javaを使用したサーブレットのテスト

私のサーブレットは、このようなものです:私はこれについてどう思うか

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

}

+0

あなたはそうです。どのような助けが必要ですか? –

+0

私はこれのテストをする必要があります...私はEasyMockを使用してそれをしようとしてきましたが、私は本当にそれを使用する方法を知らない。 – Tomarto

+0

私は自分の投稿を編集して、私がそれをやろうとしている様子を見ました – Tomarto

答えて

0

。 したがって、テストするのは難しいでしょう。 ロジックを別々のメソッドやクラスに抽出する必要があります。

関連する問題