Kotlin 瓦丁10+;,如何在测试中触发UI.getCurrent().access
我想用karibu测试来测试Vaadin流组件。在这个组件中,我使用Kotlin 瓦丁10+;,如何在测试中触发UI.getCurrent().access,kotlin,vaadin,vaadin-flow,vaadin10,Kotlin,Vaadin,Vaadin Flow,Vaadin10,我想用karibu测试来测试Vaadin流组件。在这个组件中,我使用UI.getCurrent().access{}来更新这个组件,但是在运行测试时,access中的代码不会被执行。当我尝试在测试本身中使用UI.getCurrent().access{}时,会得到相同的结果。。。一些想法 pom.xml <dependency> <groupId>com.github.mvysny.kaributesting</groupId> <artif
UI.getCurrent().access{}
来更新这个组件,但是在运行测试时,access
中的代码不会被执行。当我尝试在测试本身中使用UI.getCurrent().access{}
时,会得到相同的结果。。。一些想法
pom.xml
<dependency>
<groupId>com.github.mvysny.kaributesting</groupId>
<artifactId>karibu-testing-v10</artifactId>
<version>1.1.4</version>
</dependency>
我希望我没有误解你的问题。我试图创建一个最小的示例,其中有一个组件正在使用
UI.getCurrent().access{}
来更改UI中的某些内容
这里我有一个组件,其中有一个文本字段,值为“hallo”。该组件中有一个方法可以将TextField的值更改为“hey”
组件看起来像
package com.example.test;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.server.Command;
@Tag(value = "myComponent")
public class MyComponent extends Component {
private TextField textField = new TextField();
public MyComponent() {
textField.setValue("hallo");
}
public void changeValueToHey() {
UI.getCurrent().access((Command) () -> {
textField.setValue("hey");
});
}
public String getTextFieldValue() {
return textField.getValue();
}
}
然后我创建了一个karibu测试(1.1.6节),如:
我在文档()中找到了这些runUIQueue
,其中写道:
问题是Karibu测试在UI锁保持的情况下运行测试。
这大大简化了测试,但也防止了异步
由另一个线程更新,只是因为测试持有
锁
解决方案是在测试中简单地释放UI锁
线程,允许从后台线程发布UI.access()任务到
被处理。然后测试线程将重新获得锁和
继续测试
package com.example.test;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.server.Command;
@Tag(value = "myComponent")
public class MyComponent extends Component {
private TextField textField = new TextField();
public MyComponent() {
textField.setValue("hallo");
}
public void changeValueToHey() {
UI.getCurrent().access((Command) () -> {
textField.setValue("hey");
});
}
public String getTextFieldValue() {
return textField.getValue();
}
}
@Test
public void test() {
MyComponent myComponent = new MyComponent();
UI.getCurrent().add(myComponent);
assertEquals("hallo", myComponent.getTextFieldValue());
MockVaadin.INSTANCE.runUIQueue(true);
myComponent.changeValueToHey();
MockVaadin.INSTANCE.runUIQueue(true);
assertEquals("hey", myComponent.getTextFieldValue());
}