Unit test Struts

I have the following Struts Action:

    public ActionForward addSomething(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    SomeForm sForm = (SomeForm) form;        
    Test t = testForm.toTest();

    if (tDao.checkExistsTest(t.getTest())) {            
        return mapping.findForward("failure");          
    } else {
        t.setType(new TestType(tess));


        return mapping.findForward("success");

I did the following code to test the testAddSomethingSuccess method:

public void testAddSomethingSuccess() throws Exception {

    ActionForward forward = action.addSomething(mapping, form, request, response);

    assertEquals(tDao.getList().get(0).getTest(), "LOL");
    assertEquals(tDao.getList().get(0).getTestName(), "lol");
    assertEquals(tDao.getList().get(0).getPassword(), "12345");

    assertEquals("success", forward.getName());

How can I implement testAddClientFailed()??? :

public void testAddSomethingFailed() throws Exception {

    ActionForward forward = action.addSomething(mapping, form, request, response);

    assertEquals("failure", forward.getName());


By implementing a DAO, either concrete or via a mock, that returns false for checkExistsTest, and check the forward name as you do now--what else is there?

This is one reason why dependency injection/inversion of control is so useful.

Without knowing anything for about how you're testing, how the DAO is instantiated/etc. it's difficult to provide concrete assistance. Ultimately (hopefully obviously) to test failure, you must failure.

You need to mock tDao and make checkExistsTest return false.

In your class which has the method

   public ActionForward addSomething(...)

You should inject (via the constructor) or set via a setter method an implentation of TDao which returns false for that method e.g.

FakeDao implements TDao {
 public boolean checkExistsTest(...) {return false;}

or subclass the concrete DAO and override this method.

You could also use a mocking framework to provide the implementation like JMock or Mockito.

Need Your Help

Simple CSS: Formatting multiple tables

css html-table

I have a rather simple CSS question.

How can I convert a string from windows-1252 to utf-8 in Ruby?

windows ruby ms-access encoding character-encoding

I'm migrating some data from MS Access 2003 to MySQL 5.0 using Ruby 1.8.6 on Windows XP (writing a Rake task to do this).