假设我有一个自定义注释
@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