如何在集成测试中伪造Vaadin会话?

3

假设我有一个自定义注释

@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个回答

4

玩了一下,看起来不错,但还没有成功地将其集成到我们的应用程序中。(我认为Spring Security在某种程度上会干扰,但不确定。)等时机更加适当时,我会仔细研究一下。谢谢。 - User1291

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接