Skip to main content

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: 1250
Registered: October 2011
Senior Member
The DesktopForm of the minifig application has a method updateSummary():
protected void updateSummary() {

When I try to run a test like this (extended version of testSetName()):
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()

  Mockito.verify(form, Mockito.times(1)).updateImage();
  Mockito.verify(form, Mockito.times(2)).updateSummary();

This test is failing:
Wanted 2 times:
-> at org.eclipsescout.demo.minifigcreator.client.ui.forms.DesktopFormTest.testSetName(
But was 1 time:
-> at org.eclipsescout.demo.minifigcreator.client.ui.forms.DesktopFormTest.testSetName( 

When I put a break point in PartUtility.calculateSummary(..) I see the 2 calls:
First call:

Second call:

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: 1250
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 {

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 {

    public void doCallInitializer() throws ProcessingException {

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

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: Thu Sep 20 09:06:44 GMT 2018

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

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

Back to the top