问题描述
我正在阅读 Vaadin 教程,但我正在尝试使用 gradle 而不是 maven 来完成它,并且已经进入了“为简单的 UI 逻辑创建和运行单元测试”部分。
本节有两个测试。
一个涉及一个简单的 Java 对象,另一个涉及正在使用的表单。第一次运行,但他第二次给了我上述错误。
我怀疑 maven 有一个插件用于在这种情况下启动 ui?有没有人在 gradle 而不是 maven 中完成了教程?我的build.gradle如下。
plugins {
id 'org.springframework.boot' version '2.5.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.asciidoctor.convert' version '1.5.8'
id 'com.vaadin' version '0.14.6.0'
id 'java'
}
group = 'com.altaavionics'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
ext {
set('snippetsDir',file("build/generated-snippets"))
set('vaadinVersion',"14.6.4")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.security:spring-security-web'
implementation 'org.springframework.security:spring-security-config'
implementation 'org.hibernate.validator:hibernate-validator'
implementation 'org.flywaydb:flyway-core'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
//VAADIN
implementation ('com.vaadin:vaadin-spring-boot-starter')
//TESTING
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.vaadin:vaadin-testbench'
testImplementation 'io.github.bonigarcia:webdrivermanager:3.8.1'
testImplementation 'org.junit.vintage:junit-vintage-engine'
// testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
// testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}
dependencyManagement {
imports {
mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
}
}
test {
outputs.dir snippetsDir
useJUnitPlatform()
}
asciidoctor {
inputs.dir snippetsDir
dependsOn test
}
我的测试课和教程一样,但在这里
package com.vaadin.tutorial.crm.ui.views.list;
import com.vaadin.tutorial.crm.backend.entity.Company;
import com.vaadin.tutorial.crm.backend.entity.Contact;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.atomicreference;
public class ContactFormTest {
private List<Company> companies;
private Contact marcUsher;
private Company company1;
private Company company2;
@Before
public void setupData() {
companies = new ArrayList<>();
company1 = new Company("Vaadin Ltd");
company2 = new Company("IT Mill");
companies.add(company1);
companies.add(company2);
marcUsher = new Contact();
marcUsher.setFirstName("marc");
marcUsher.setLastName("Usher");
marcUsher.setEmail("marc@usher.com");
marcUsher.setStatus(Contact.Status.NotContacted);
marcUsher.setCompany(company2);
}
@Test
public void formFieldsPopulated() {
ContactForm form = new ContactForm(companies);
form.setContact(marcUsher);
Assert.assertEquals("marc",form.firstName.getValue());
Assert.assertEquals("Usher",form.lastName.getValue());
Assert.assertEquals("marc@usher.com",form.email.getValue());
Assert.assertEquals(company2,form.company.getValue());
Assert.assertEquals(Contact.Status.NotContacted,form.status.getValue());
}
@Test
public void saveEventHasCorrectValues() {
ContactForm form = new ContactForm(companies);
Contact contact = new Contact();
form.setContact(contact);
form.firstName.setValue("John");
form.lastName.setValue("Doe");
form.company.setValue(company1);
form.email.setValue("john@doe.com");
form.status.setValue(Contact.Status.Customer);
atomicreference<Contact> savedContactRef = new atomicreference<>(null);
form.addListener(ContactForm.SaveEvent.class,e -> {
savedContactRef.set(e.getContact());
});
form.save.click();
Contact savedContact = savedContactRef.get();
Assert.assertEquals("John",savedContact.getFirstName());
Assert.assertEquals("Doe",savedContact.getLastName());
Assert.assertEquals("john@doe.com",savedContact.getEmail());
Assert.assertEquals(company1,savedContact.getCompany());
Assert.assertEquals(Contact.Status.Customer,savedContact.getStatus());
}
}
解决方法
由于异常堆栈跟踪和测试代码都丢失了,我只能猜测。通常你通过运行你的应用程序(通过使用一个特殊的 Gradle 插件)来测试你的应用程序,然后通过 Selenium/TestBench 访问服务器。但是,这种方法用于集成/系统测试,并且您明确提到了单元测试。对于这些,需要进行一些设置,否则 UI.getCurrent()
将返回 null。
要轻松执行此设置,请查看 https://github.com/mvysny/karibu-testing 。有大量文档,包括基于 Gradle 的示例项目。