Andrew said:
I take it the list is the standard Java one?
Yes.
And you are attempting to create a mock version of it?
Yes. I'm mocking the response an instance of List would return to the
isEmpty() API.
Because I don't want to create an actual instance and have to populate
it with actual instances of my WebForm object. Hence the reason I'm
using EasyMock.
We don't need to mock things that work or are easy to use.
We need to mock those things which we do not want to have to actually
create. I don't want to create an instance of List<WebForm> because to
do so means I would then have to create actual instances of WebForm and
add them to the List when I want to have the isEmpty() method return false.
I'd guess for your test, its the contents you don't care about but you
do care that a valid list object is used. So use a list, but fill it
with mock contents.
Sorry, but you should really learn more about my requirements first
before saying whether I should or shouldn't mock a List or its
contents.. I don't care about the contents and, since I'm not writing
unit tests for java.util.List, dont' care about the concrete
implementations available for List. I only need to have a mock control
(in this case) either return true or false when the isEmpty() method is
invoked.
Can you post your test case code? ( I don't need to see the code its
testing)
Sure. Here ya go:
---8<[snip]---
package com.redhat.
www.webforms.web;
import java.util.List;
import org.easymock.MockControl;
import org.springframework.validation.Errors;
import com.redhat.
www.webforms.domain.WebForm;
import com.redhat.
www.webforms.domain.WebFormListCommand;
import junit.framework.TestCase;
/**
* <code>TestWebFormListValidator</code> performs unit tests on the
* {@link WebFormListValidator} class.
*
* @author Darryl L. Pierce
*/
public class TestWebFormListValidator extends TestCase
{
private WebFormListValidator validator;
private WebFormListCommand command;
private MockControl<Errors> controlErrors;
private Errors errors;
private MockControl<? extends List> controlWebforms;
private List<WebForm> webforms;
private MockControl<? extends List> controlSelectedWebForms;
private List<WebForm> selectedWebForms;
protected void setUp() throws Exception
{
validator = new WebFormListValidator();
command = new WebFormListCommand();
controlErrors = MockControl.createControl(Errors.class);
errors = controlErrors.getMock();
controlWebforms = MockControl.createControl(List.class);
webforms = controlWebforms.getMock();
command.setWebforms(webforms);
controlSelectedWebForms =
MockControl.createControl(List.class);
selectedWebForms = controlSelectedWebForms.getMock();
command.setSelectedWebforms(selectedWebForms);
}
protected void tearDown() throws Exception
{
controlErrors.verify();
controlWebforms.verify();
controlSelectedWebForms.verify();
}
private void replayControls()
{
controlErrors.replay();
controlWebforms.replay();
controlSelectedWebForms.replay();
}
/**
* Tests the validator's response to a delete request without any forms
* present.
*/
public void testDeleteWithNoFormsPresent()
{
webforms.isEmpty();
controlWebforms.setReturnValue(true);
errors.reject("webform.error.delete-no-forms-present");
controlErrors.setVoidCallable();
replayControls();
validator.validate(command, errors);
}
/**
* Tests the validator's response to a delete request without any forms
* selected.
*/
public void testDeleteWithoutSelectedForms()
{
webforms.isEmpty();
controlWebforms.setReturnValue(false);
selectedWebForms.isEmpty();
controlSelectedWebForms.setReturnValue(true);
errors.reject("webform.required.delete-requires-selection");
controlErrors.setVoidCallable();
replayControls();
validator.validate(command, errors);
}
/**
* Test the validator's response to a delete request with at least
one form
* selected.
*/
public void testDeleteWithFormsSelected()
{
webforms.isEmpty();
controlWebforms.setReturnValue(false);
selectedWebForms.isEmpty();
controlSelectedWebForms.setReturnValue(false);
replayControls();
validator.validate(command, errors);
}
}
---8<[snip]---