João Mendes
João Mendes

Reputation: 1435

Wicket AJAX unit test passes on its own but fails in the full test suite

I have a test class that runs fine on its own and when I run the whole of package it's in. However, it fails 4 out of its 20 tests when I run the whole project.

I'm using Eclipse Juno SR1 to run the tests, and I'm using EasyMock 3.2. (PowerMock is also in the project setup, but it's only being used in two tests where it is specifically needed to mock an external dependency.) The project is in Java 7, using Wicket 6.18.

I'm sure you need lots more information, but I have no idea where to begin, so please, if you need to know anything else, comment and I'll (try to) add it.

Here's the failure message for one of them:

junit.framework.AssertionFailedError: Missing expected feedback message: The value of 'percentage' must be between 0 and 100.

The failure message for the other three is also related to missing feedback messages. The behavior is deterministic. It's always the same four test cases in the same test class that fail, always with the same "missing expected feedback" message.

Here's one of the test cases:

@Test
public void testPercentageTooLarge() {
    UserApplication u = new UserApplication();
    setUser(u);

    IEditIntervalDTOFrontEnd mockModel = createStrictMock(IEditIntervalDTOFrontEnd.class);
    expect(mockModel.getLowerBound()).andReturn(BigDecimal.ZERO);
    expect(mockModel.getUpperBound()).andReturn(BigDecimal.ZERO);
    expect(mockModel.isLast()).andReturn(false);
    expect(mockModel.getLowerLimit()).andReturn(BigDecimal.ZERO);
    expect(mockModel.getUpperLimit()).andReturn(BigDecimal.ZERO);
    expect(mockModel.getPercentage()).andReturn(new BigDecimal("3"));

    replayAll();

    EditIntervalPanel p = new EditIntervalPanel("test", new Model<IEditIntervalDTOFrontEnd>(mockModel));
    startTest(p, false);

    replay(getTarget());

    @SuppressWarnings("unchecked")
    TextField<BigDecimal> t = (TextField<BigDecimal>)getTester().getComponentFromLastRenderedPage(p.getPageRelativePath() + ":form:percentageContainer:percentage");
    getTester().getRequest().getPostParameters().setParameterValue(t.getInputName(), "500");
    getTester().executeAjaxEvent(t.getPageRelativePath(), "onchange");

    verifyAll();

    getTester().assertComponentOnAjaxResponse(p.getPageRelativePath() + ":form:errorPanel");
    getTester().assertComponent(p.getPageRelativePath() + ":form:errorPanel", BootstrapFeedbackPanel.class);
    getTester().assertFeedback(p.getPageRelativePath() + ":form:errorPanel", "The value of 'percentage' must be between 0 and 100.");
}

The other three are similar. They inject invalid values into the request parameters, then invoke the AJAX onchange event.

Here's a bit of the component being tested:

    TextField<BigDecimal> percentage = new TextField<BigDecimal>("percentage", new PropertyModel<BigDecimal>(getModel(), "percentage"));
    percentage.setRequired(true);
    percentage.add(new RangeValidator<BigDecimal>(BigDecimal.ZERO, new BigDecimal(100)));
    form.add(new WebMarkupContainer("percentageContainer").add(percentage));
    percentage.add(new AjaxFormComponentUpdatingBehavior("onChange") {
        private static final long serialVersionUID = 1L;
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            try {
                if (!getModelObject().isNew()) {
                    getModelObject().execAndSync(((TemplatePage)getPage()).getLoggedInUser());
                    notifyChanged(target);
                }
            } catch (DTOFrontEndException e) {
                logger.error(e.getMessage(), e);
                throw new WicketRuntimeException(e.getMessage(), e);
            }
            target.add(errorPanel);
        }

        @Override
        protected void onError(AjaxRequestTarget target, RuntimeException e) {
            target.add(errorPanel);
        }
    });

Here's the interface being mocked, in case it helps, for some reason:

public interface IEditIntervalDTOFrontEnd extends IDTOFrontEnd {
    BigDecimal getLowerBound();
    BigDecimal getLowerLimit();
    BigDecimal getUpperLimit();
    BigDecimal getUpperBound();
    BigDecimal getPercentage();
    boolean isLast();
    boolean isNew();

    void setUpperLimit(BigDecimal value);
    void setPercentage(BigDecimal value);
    boolean isWellOrdered();

    void reorder(IEditIntervalDTOFrontEnd prev, IEditIntervalDTOFrontEnd next);
    boolean isChanged();
}

Upvotes: 0

Views: 78

Answers (1)

martin-g
martin-g

Reputation: 17513

I don't see anything wrong. Try by dumping the last response: System.err.println("===DEBUG====\n\n" + getTester.getLastResponse().getDocument());. Check what is the value of the TextField and what is the generated content for the FeedbackPanel.

Upvotes: 1

Related Questions