使用JUNIT但不用于测试?

发布于 2025-02-03 21:22:46 字数 185 浏览 2 评论 0原文

我有一个带有Vaadin的Spring Boot应用程序,我只需要简单的UI元素验证而无需备份BEAN。例如,仅检查TextField值是否为空,是否为空。

由于验证者需要一个bean,因此我正在考虑将Junit断言用于这项简单的工作。因此,不仅将JUNIT用于运行测试,还用于应用程序的主要流程。

这是好的做法,还有什么选择?

I have a spring boot application with Vaadin and I just need simple UI element validation without backing beans.For example to just check if a textfield value is null,empty or not.

Since validators require a bean I was thinking of using Junit Assert for this simple job. So using Junit not just for running tests but for the main flow of the application.

Is this good practice and what alternatives are there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑疤怪 2025-02-10 21:22:46

对于您需要将vaadin的转换器或验证器绑定到单个字段/值对的情况下,我已经写了 fieldBinder 实用程序类,可以在vaadin的目录中找到,并添加为对项目的Maven依赖性。

https://vaadin.com/directory/component/component/fieldbinds/fieldbinder

        // Binder with integer
    FieldBinder<Integer> integerFieldBinder = new FieldBinder<>();
    TextField integerField = new TextField("Input number");

    // Text field with Integer value Converter and Validator
    // Demoing how to detect if value is valid and how to get it from FieldBinding
    FieldBinding<Integer> integerBinding = integerFieldBinder.forField(integerField)
            .withConverter(new StringToIntegerConverter("This is not a number"))
            .withValidator(new IntegerRangeValidator("Give a number between 5 and 10",5,10))
            .bind(integerValue);

For such use cases where you need to bind Vaadin's Converters or Validators to a single field / value pair without a Bean being involved I have written FieldBinder utility class, which can be found in Vaadin's Directory and added as maven dependency to your project.

https://vaadin.com/directory/component/fieldbinder

        // Binder with integer
    FieldBinder<Integer> integerFieldBinder = new FieldBinder<>();
    TextField integerField = new TextField("Input number");

    // Text field with Integer value Converter and Validator
    // Demoing how to detect if value is valid and how to get it from FieldBinding
    FieldBinding<Integer> integerBinding = integerFieldBinder.forField(integerField)
            .withConverter(new StringToIntegerConverter("This is not a number"))
            .withValidator(new IntegerRangeValidator("Give a number between 5 and 10",5,10))
            .bind(integerValue);
滴情不沾 2025-02-10 21:22:46

这种好习惯

不是 junit 是否要在生产代码中使用

我只需要简单的UI元素验证而无需备份豆。
有什么选择

仍然可以使用绑定的API,只忽略Getters/setters:

new Binder<>().forField(new TextField())
  .withValidator(...)
  .bind(o -> null, (o, o2) -> {});

Is this good practice

I don't think junit is meant to be used in a production code

I just need simple UI element validation without backing beans.
what alternatives are there

I believe you can still use the binding API, just ignore getters/setters:

new Binder<>().forField(new TextField())
  .withValidator(...)
  .bind(o -> null, (o, o2) -> {});
回眸一笑 2025-02-10 21:22:46

虽然binder需要“ bean”不必是
一个完全抽出的东西。您还可以使用两个功能绑定(a
valueProvidersetter)。因此,您基本上可以使用任何
容器您喜欢作为“ bean”。例如a 地图atomicInteger(您
必须具有从中读取值并回写值的手段)。

def tf = new TextField("Answer to all questions of the universe").tap {
    setWidthFull()
}

def binder = new Binder<Map>().tap{
    forField(tf)
        .asRequired("A value is mandatory")
        .withNullRepresentation("")
        .withConverter(new StringToIntegerConverter("Only numbers are allowed"))
        .withValidator(new IntegerRangeValidator("Must be 42", 42, 42))
        .bind( // XXX
            { m -> m.value },
            { m, v -> m.value = v }
        )

    setBean([:])
}

add(tf)
add(new Button("Save", {
    Notification.show(binder.valid ? "Saved ${binder.bean}" : "Please fix the errors first")
}))

While a "bean" is needed for the Binder it does not have to be
a something fully drawn out. You can also bind with two functions (a
ValueProvider and a Setter). With this you can basically use any
container you like as "bean". E.g. a Map or an AtomicInteger (you
must have means to read a value from it and write a value back).

def tf = new TextField("Answer to all questions of the universe").tap {
    setWidthFull()
}

def binder = new Binder<Map>().tap{
    forField(tf)
        .asRequired("A value is mandatory")
        .withNullRepresentation("")
        .withConverter(new StringToIntegerConverter("Only numbers are allowed"))
        .withValidator(new IntegerRangeValidator("Must be 42", 42, 42))
        .bind( // XXX
            { m -> m.value },
            { m, v -> m.value = v }
        )

    setBean([:])
}

add(tf)
add(new Button("Save", {
    Notification.show(binder.valid ? "Saved ${binder.bean}" : "Please fix the errors first")
}))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文