gpt4 book ai didi

kotlin - Vaadin 10+,如何在kaributesting中触发UI.getCurrent()。access

转载 作者:行者123 更新时间:2023-12-02 12:41:30 26 4
gpt4 key购买 nike

我想使用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的值更改为“嘿”。

该组件看起来像

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版),如下所示:
@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.

关于kotlin - Vaadin 10+,如何在kaributesting中触发UI.getCurrent()。access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56643753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com