Reputation: 8182
Let's say I have a custom annotation
@ViewScope //<-- the issue
@Qualifier
@Component
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Foo { ... }
I now want to test a method that is supposed to retrieve all the beans with that annotation.
How do I fake the vaadin session s.t. such an integration test
@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)
}
}
succeeds instead of crashing with
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
Upvotes: 3
Views: 367
Reputation: 5342
Give Karibu Testing
a shot
https://github.com/mvysny/karibu-testing/tree/master/karibu-testing-v8
It has methods for mocking a Vaadin session, it supports V8 as well as V10+, and it even supports Kotlin!
Upvotes: 4