Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » Mockito Spy and inner classes
Mockito Spy and inner classes [message #1447387] Sat, 18 October 2014 07:12 Go to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 1243
Registered: October 2011
Senior Member
The DesktopForm of the minifig application has a method updateSummary():
protected void updateSummary() {
  getSummaryField().setValue(PartUtility.calculateSummary(
      getNameField().getValue(),
      getHeadField().getValue(),
      getTorsoField().getValue(),
      getLegsField().getValue()
      ));
}


When I try to run a test like this (extended version of testSetName()):
@Test
public void testSetName() throws Exception {
  DesktopForm form = Mockito.spy(createFormWithState(true, true, true));
  form.startView(); //<--this calls updateSummary() in ViewHandler.execLoad() [using reloadForm()]

  form.getNameField().setValue("Bob"); //<-- this calls updateSummary() in NameField.execChangedValue()

  Assert.assertTrue(form.getSummaryField().getValue().startsWith("Bob"));
  Mockito.verify(form, Mockito.times(1)).updateImage();
  Mockito.verify(form, Mockito.times(2)).updateSummary();
}


This test is failing:
 org.mockito.exceptions.verification.TooLittleActualInvocations: 
desktopForm.updateSummary();
Wanted 2 times:
-> at org.eclipsescout.demo.minifigcreator.client.ui.forms.DesktopFormTest.testSetName(DesktopFormTest.java:146)
But was 1 time:
-> at org.eclipsescout.demo.minifigcreator.client.ui.forms.DesktopFormTest.testSetName(DesktopFormTest.java:140) 


When I put a break point in PartUtility.calculateSummary(..) I see the 2 calls:
First call:
index.php/fa/19565/0/

Second call:
index.php/fa/19566/0/

In the second call, the DesktopForm is not instrumented by Mockito. This is why the spy do not think that updateSummary() was called twice.

Does Mockito have a problem with the scout inner-classes?

.
Re: Mockito Spy and inner classes [message #1449276 is a reply to message #1447387] Tue, 21 October 2014 05:47 Go to previous message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 1243
Registered: October 2011
Senior Member
André Wegmüller gave me following tipp: I should check when the search for field inner is made. The form keeps references to inner-classes in private members. This search should occur after the Mockito instrumentation.

This is exactly my problem!

The AbstractForm has a constructor with a Boolean argument callInitializer. If called with false, the form needs to call the protected method callInitializer() latter on.

This is what I did:

1/ add a protected constructor in my Desktop Form:
  protected DesktopForm(boolean callInitializer) throws ProcessingException {
    super(callInitializer);
  }


2/ add a new class in the test case to make the callInitializer() accessible and to call the new constructor
  public static class P_DesktopForm extends DesktopForm {
    public P_DesktopForm() throws ProcessingException {
      super(false);
    }

    public void doCallInitializer() throws ProcessingException {
      super.callInitializer();
    }
  }


3/ In the test case, instantiate the Desktop Form like this:
 P_DesktopForm form = Mockito.spy(new P_DesktopForm());
form.doCallInitializer(); 


And now my test is working.
Previous Topic:Waiting for a job in a unit test?
Next Topic:The whole view button menu (AbstractOutlineViewButton) is lost after some time
Goto Forum:
  


Current Time: Sat Oct 21 14:24:59 GMT 2017

Powered by FUDForum. Page generated in 0.03293 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software