Search code examples
eclipse-scout

Eclipse Scout Neon unit test of execChangedMasterValue


I have two fields that are connected with master-slave relationship :

public class Slave extends AbstractListBox<String> {

  @Override
  protected Class<? extends IValueField> getConfiguredMasterField() {

    return Master.class;
  }

  @Override
  protected void execChangedMasterValue(final Object newMasterValue) {
    this.function() // -> here I put debugging break point
  }
}

public class Master extends AbstractBooleanField {

  @Override
  protected void execChangedValue() {

    super.execChangedValue(); // -> Break point 2 
  }
}

I write unit test for this relationship, but inside unit test execChangedMasterValue is never called.

My unit test looks like :

@Test
public void test() {

    this.box.getMaster.setValue(true)
    Assert.assertFalse(... something from function Slave ...)
}

Unit tests always failed, and if I put breakpoints as described above, debugger stops only on second break point but never on first one.

In "real" world, function is called and everything works as it should.

Is there a reason that execChangedMasterValue is not called? Is behaviour of execChangedMasterValue different from changedValue()?


Solution

  • I cannot reproduce what you are describing. I took the Scout HelloWorld Project (the one that is generated when you create a new project).

    In the HelloWorldForm I have added this "slave" field in the MainBox:

    @Order(2000)
    public class LengthField extends AbstractIntegerField {
        @Override
        protected String getConfiguredLabel() {
            return TEXTS.get("Length");
        }
    
        @Override
        protected Class<? extends IValueField<?>> getConfiguredMasterField() {
            return MessageField.class;
        }
    
        @Override
        protected void execChangedMasterValue(Object newMasterValue) {
            if(newMasterValue instanceof String) {
                String s = (String) newMasterValue;
                setValue(s.length());
            } else {
                setValue(0);
            }
        }
    }
    

    And in the example Unit Test HelloWorldFormTest, I have added an additional check to testMessageCorrectlyImported():

    /**
     * Tests that the {@link MessageField} is correctly filled after start.
     */
    @Test
    public void testMessageCorrectlyImported() {
        HelloWorldForm frm = new HelloWorldForm();
        frm.start();
    
        Assert.assertEquals("Message field", MESSAGE_VALUE, frm.getMessageField().getValue());
        Assert.assertEquals("Length field", Integer.valueOf(MESSAGE_VALUE.length()) , frm.getLengthField().getValue());
    
        frm.getMessageField().setValue("abcdef");
        Assert.assertEquals("Length field (after update)", Integer.valueOf("abcdef".length()), frm.getLengthField().getValue());
    }
    

    Everything works as expected…