如何为集成测试创建模拟 VaadinSession? [英] How to create mock VaadinSession for integration tests?

查看:29
本文介绍了如何为集成测试创建模拟 VaadinSession?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:

这是我第一次对 Vaadin UI 进行测试,而且我对单元测试也很陌生.我的问题是我不能对我的 UI 组件视图做任何事情,因为没有处理 UI bean 的 VaadinSession.使用 @SpringBootTest 时永远不会创建 VaadinSession.由于 Spring 处理这些 bean,我能够为我的后端创建测试,但我似乎无法找到让 Vaadin 启动会话的方法,这样我就可以访问会话并进行不同的集成和单元测试.

This is my first time doing tests for Vaadin UI and I am also fairly new to Unit tests in general. My issue is that I can't do anything with my UI components Views because there is no VaadinSession which handles the UI beans. A VaadinSession is never created when using @SpringBootTest. I was able to create tests for my backend since Spring handles those beans, but I can't seem to figure out a way to get Vaadin to start up a session so I can then access the session and do different integration and unit tests.

我的尝试

TestBench: Vaadin 测试台似乎是一个非常好的选择,但我面临的问题是,每当我打开访问网站的 ChromeDriver() 时,它似乎都没有打开 VaadinSession在我的本地主机上.

TestBench: The Vaadin testbench seemed like a very good option but the issue that I faced was that it doesn't seem to open a VaadinSession whenever I open a ChromeDriver() that goes to the website on my localhost.

卡里布图书馆: 这个库似乎是一个很好的选择,但有一个问题,那就是它打开了实例化的单个 UI 组件,但是我的几个 UI 组件类使用依赖注入来注入后端服务.由于依赖注入,我无法实例化这些类.

Karibu Library: This library seemed like a very good option, but there was one issue, which was that it opens individual UI components that are instantiated, however a couple of my UI Components Classes, uses dependency injection to inject backend services. I cannot instantiate these classes because of the dependeny injection.

我需要通过 VaadinSession 访问的 UI 组件.

@Component
@UIScope
@CssImport("./styles/current-info-styles.css")
public class CurrentDayView extends VerticalLayout implements Updatable {
    private static final long serialVersionUID = 1L;

    //Some code here

    @Autowired
    public CurrentDayView(NowcastWeatherService nowcastWeatherService, GeoLocationService geoLocationService) {
        this.nowcastWeatherService = nowcastWeatherService;
        this.geoLocationService = geoLocationService;

        //Some Code here
    }
   //Some code here

我的测试平台方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class CurrentDayViewTest extends TestBenchTestCase {

    @Test
    public void fakeTest() {
        Assert.assertTrue(true);
    }

    @Before
    public void startUp() {
        System.setProperty("webdriver.chrome.driver", "src/main/resources/drivers/chromedriver.exe");
        setDriver(new ChromeDriver());
        getDriver().get("http://localhost:8080/");

        populateViewWithInformation();
    }

    @After
    public void tearDown() {
        getDriver().quit();
    }

    private void populateViewWithInformation() {
        CurrentDayView currentDayView = (CurrentDayView) VaadinSession.getCurrent().getAttribute("current-day-view");
        //This is where I get an error because VaadinSession.getCurrent() is null
    }

我的最后一个问题:

有没有人知道我如何创建 VaadinSession 或至少让 Spring 跟踪 Vaadin UI 组件?如果这还不清楚,请随时就我的问题提出更多说明.

Does anyone have any idea on how I could have a VaadinSession created or atleast get spring to keep track for Vaadin UI components? If this wasn't clear then please feel free to ask more clarifications relating to my question.

推荐答案

我建议你再试一次 Karibu,它非常适合做这类不需要运行应用程序的测试.

I suggest you give Karibu another shot, it's great for doing these kinds of tests that don't need the app to be running.

查看 Karibu V14 Spring 演示项目.注意使用什么 Karibu 依赖.ApplicationTest#listOrders 测试包含导航到具有自动装配依赖项的视图.

Take a look at the Karibu V14 Spring demo project. Pay attention to what Karibu dependency is used. The ApplicationTest#listOrders test contains navigation to a view with autowired dependencies.

您的 TestBench 测试的问题在于 TestBench 用于针对正在运行的应用程序进行测试,而这些测试在与实际应用程序完全不同的过程中运行.

The issue with your TestBench test is that TestBench is used to test against a running application, and the tests run in an entirely different process than the actual application.

因此,当您使用驱动程序打开页面时,会在应用程序中创建一个 Vaadin 会话,但您将无法在测试中访问它,也无法访问任何 UI 状态或视图.然而,它允许您做的是与应用程序进行交互,就像您通过浏览器所做的那样(单击按钮、填写文本字段等),并检查浏览器中的状态是否正确,而无需了解有关服务器的内部状态.

So when you open the page with the driver, a Vaadin session is created in the application, but you will not be able to access it in your tests, nor will you be able to access any UI state or views. What it allows you to do, however, is to interact with the application as you would do through the browser (clicking buttons, filling in text fields etc.), and to check that the state in the browser is correct, without knowing anything about the server's internal state.

这篇关于如何为集成测试创建模拟 VaadinSession?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆