Vaadin 10+,如何在 kaributesting 中触发 UI.getCurrent().access
Vaadin 10+, How to trigger UI.getCurrent().access in kaributesting
我想用 karibu-testing 测试 Vaadin 流组件。在这个组件中,我使用 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>
测试(Kotlin)
class MyUITest {
@BeforeAll
fun init() {
MockVaadin.setup()
}
@BeforeEach
fun setup() {
UI.getCurrent().removeAll()
}
@Test
fun testSometing() {
UI.getCurrent().access {
print("foo") // this line is not reachable
}
}
}
希望我没有误解你的问题。我尝试创建一个最小的示例,其中包含一个组件,该组件使用 UI.getCurrent().access {}
来更改 UI.
中的某些内容
这里我有一个组件,其中有一个 TextField,其值为 "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 测试 (v. 1.1.6),例如:
@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());
}
我在文档 (https://github.com/mvysny/karibu-testing/tree/master/karibu-testing-v10) 中找到了那些 runUIQueue
,其中说:
The thing is that Karibu Testing runs the test with the UI lock held.
That simplifies testing very much, but that also prevents async
updates by another thread, simply because the test is holding the
lock!
The solution is to briefly let loose of the UI lock in the testing
thread, allowing UI.access() tasks posted from a background thread to
be processed. Then the testing thread will re-obtain the lock and
continue testing.
我想用 karibu-testing 测试 Vaadin 流组件。在这个组件中,我使用 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>
测试(Kotlin)
class MyUITest {
@BeforeAll
fun init() {
MockVaadin.setup()
}
@BeforeEach
fun setup() {
UI.getCurrent().removeAll()
}
@Test
fun testSometing() {
UI.getCurrent().access {
print("foo") // this line is not reachable
}
}
}
希望我没有误解你的问题。我尝试创建一个最小的示例,其中包含一个组件,该组件使用 UI.getCurrent().access {}
来更改 UI.
这里我有一个组件,其中有一个 TextField,其值为 "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 测试 (v. 1.1.6),例如:
@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());
}
我在文档 (https://github.com/mvysny/karibu-testing/tree/master/karibu-testing-v10) 中找到了那些 runUIQueue
,其中说:
The thing is that Karibu Testing runs the test with the UI lock held. That simplifies testing very much, but that also prevents async updates by another thread, simply because the test is holding the lock!
The solution is to briefly let loose of the UI lock in the testing thread, allowing UI.access() tasks posted from a background thread to be processed. Then the testing thread will re-obtain the lock and continue testing.