您如何伪造vaadin会话进行集成测试?

时间:2019-05-20 10:51:19

标签: java spring kotlin vaadin vaadin8

假设我有一个自定义注释

@ViewScope //<-- the issue
@Qualifier
@Component
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Foo { ... }

我现在想测试一种方法,该方法应该检索带有该批注的所有bean。

我如何伪造vaadin会话这样的集成测试

@RunWith(SpringJUnit4ClassRunner::class)
@SpringBootTest
@WebAppConfiguration
class FooAnnotationIT{

    @Autowired(required = false) //required=false --> empty list instead of exception when none are found
    @Foo
    val foos:List<Any> = mutableListOf()

    @Test
    fun `all beans are present`(){
        assertThat(foos.size).isEqualTo(5)
    }
}

成功而不是崩溃

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'blah.blah.blah.FooAnnotationIT': Unsatisfied dependency expressed through field 'foos'
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewCache': Scope 'vaadin-ui' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton
    Caused by: java.lang.IllegalStateException: No VaadinSession bound to current thread

1 个答案:

答案 0 :(得分:4)

Karibu Testing一个镜头 https://github.com/mvysny/karibu-testing/tree/master/karibu-testing-v8

它具有模拟Vaadin会话的方法,它支持V8和V10 +,甚至还支持Kotlin!