-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ComponentEvent.isFromClient() is false despite using TestWrappers.test() in a SpringUIUnitTest #1814
Comments
True, the problem is here The tester should call with introspection setModelValue(value, true) instead. |
I checked this further ... It looks like it is problem with all the fields. The trouble is that not all of them are as easy to fix as the fields directly using I first thought that I can simply write utility method that is used by all field Testers to set value, like below:
Which would allow me to rewrite
This works for |
I've used even wilder reflection which works for all AbstractFields: public class FlowUtils {
@NotNull
private static <V> AbstractFieldSupport<?, V> getFieldSupport(@NotNull HasValue<?, V> component) {
try {
final Field javaField = AbstractField.class.getDeclaredField("fieldSupport");
javaField.setAccessible(true);
return (AbstractFieldSupport<?, V>) javaField.get(component);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Sets the value to given component. Supports pretending that the value came from the browser.
* @param component the component to set the value to, not null.
* @param value the new value, may be null.
* @param isFromClient if true, we'll pretend that the value came from the browser. This causes the value change event to be fired
* with `isFromClient` set to true.
* @param <V> the value type
*/
public static <V> void setValue(@NotNull HasValue<?, V> component, @Nullable V value, boolean isFromClient) {
if (!isFromClient) {
component.setValue(value);
return;
}
if (component instanceof AbstractField) {
final AbstractFieldSupport<?, V> fs = getFieldSupport(component);
try {
final Method m = AbstractFieldSupport.class.getDeclaredMethod("setValue", Object.class, boolean.class, boolean.class);
m.setAccessible(true);
m.invoke(fs, value, false, isFromClient);
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException(e);
}
return;
}
throw new IllegalArgumentException("Parameter component: invalid value " + component + ": unsupported type of HasValue: " + component.getClass());
}
} |
I expect
ComponentEvent.isFromClient()
to return true when usingTestWrappers.test()
in aSpringUIUnitTest
to execute the action, that triggers theComponentEvent
.Instead it returns false.
Steps to reproduce
MainView.java
MainViewTest.java
Expected Result
event.isFromClient()
should return true, whenTestWrappers.test()
is used to set the value of aTextField
.Then
MainViewTest#whenSetTextThenSetLabel
would complete successfuly.Actual Result
MainViewTest#whenSetTextThenSetLabel
fails with thisAssertionError
:Versions
Vaadin 24.4.6
Java 17
macOS 14.5
The text was updated successfully, but these errors were encountered: