Testing View

I have a MainView which has several Repositories in the constructor. How do I write JUnit Tests for that? Does anybody have an example for that? So I don’t know how to set it up with the repository classes. And I have a class with annotation @UIScope which is also making problems, any ideas for that as well?

It all ought to go as with other Spring managed beans. How are you running the tests? Are you using @SpringBootTest etc ? Testing Vaadin Flow Applications with jUnit

The problem is with the scope, it throws a ScopeNotActiveException and I tried creating a MainView with the annotations and added 2 Items to a List and called a method that is supposed to add some more items based on the items in the list but it remains at size 2

If you think it’s a bug please fill a issue with a minimal example here https://github.com/vaadin/testbench

I just understood one part, but I am still having the ScopeNotActiveException. I have a class called CurrentProject which is used in the constructor of my MainView and CurrentProject has “@UIScope” annotation. ( I use it to save the latest progress if you switch between the views). What changes need to be done in the test class to get around this? And does somebody have examples of MultiSelectComboBox tests? I tried using setValue but get an UI Instance is not available error

Why is this guide still JUnit4?

I replace UIScope and VaadinScope:

class VaadinScopedBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
        for (String beanName : factory.getBeanDefinitionNames()) {
            BeanDefinition beanDef = factory.getBeanDefinition(beanName);
            String explicitScope = beanDef.getScope();
            if ("vaadin-ui".equals(explicitScope)) {
                beanDef.setScope("singleton");
            }
            if ("vaadin-session".equals(explicitScope)) {
                beanDef.setScope("singleton");
            }
        }
    }
}

And have my own test configuration

@Configuration
public class VaadinTestConfiguration {

    @Bean
    public BeanFactoryPostProcessor beanFactoryPostProcessor() {
        return new VaadinScopedBeanFactoryPostProcessor();
    }
}

That’s a good question, especially as the testing section was just recently worked on with the new TestBench features. Should default to 5 now.

The actual reference doc part seems to be more up to date with both junit 4 & 5 instructions: Getting Started | UI Unit Testing | Testing | Vaadin Docs

@adaptable-uakari The tutorial uses browser tests using TestBench, which is somewhat tied to JUnit 4. Might be the case why that is still in JUnit 4 :thinking:

The one mentioned before does not use TestBench Testing Vaadin Flow Applications with jUnit

Yup, but in the next step we’d need to downgrade all that to JUnit 4 when throwing in TestBench :thinking:

Or maybe just accept to have both in the same project :man_shrugging:

I’m using Testbench with Junit 5 tho :face_with_monocle:

I use Junit4 for TestBench and Junit5 for all other tests

I heard that some features are not compatible with Junit 5.

I am pretty new to this stuff, so could you tell me what steps I need to do to use this? Is the “@Configuration” class my Test class where I have the tests with “@Test” annotation or do I call the class with “@Configuration” from my test class or how does this work?

@adaptable-uakari I created patches to upgrade the tutorial to Junit 5, let me know what you think: Changed to use JUnit 5 by mstahv · Pull Request #1802 · vaadin/docs · GitHub

I’m thinking about adding “@ExtendWith(SpringExtension.class)” which is used if someone wants to create a common base class like often recommended even in the Testbench docs