testbench icon indicating copy to clipboard operation
testbench copied to clipboard

Provide a way to access VaadinSession in testcase

Open mkemmerz opened this issue 5 years ago • 0 comments

I had this issue in one of our projects and was able to reproduce it in the testbench-demo (https://github.com/vaadin/testbench-demo).

Vaadin Version in Testbench-demo: 10.0.1 Vaadin Version in our project: 10.0.10

How to reproduce:

  • Execute a testcase with vaadin testbench (for example checkout https://github.com/vaadin/testbench-demo and adjust the test like the code example shows)
  • Try to get the Session using VaadinSession.getCurrent() -> NPE

Code example (adjusted from https://github.com/vaadin/testbench-demo/blob/master/src/test/java/com/vaadin/testbenchexample/pageobjectexample/CalcViewIT.java):

    @Test
    public void resultIsLogged() throws Exception {
        if (VaadinSession.getCurrent() == null)
            System.out.println("VaadinSession is null?");

        WrappedSession session = VaadinSession.getCurrent().getSession(); // Throws NPE
        keypad.calculate("1+2");
        assertEquals("1.0 + 2.0 = 3.0", getLog().getRow(0));
    }
resultIsLogged(com.vaadin.testbenchexample.pageobjectexample.CalcViewIT)  Time elapsed: 4.347 sec  <<< ERROR!
java.lang.NullPointerException
        at com.vaadin.testbenchexample.pageobjectexample.CalcViewIT.resultIsLogged(CalcViewIT.java:42)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:367)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:274)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:161)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)

mkemmerz avatar Oct 29 '19 07:10 mkemmerz