Unit test fails in gradle test task

I have written a Unit test class as follow. I can run the test in IDEA by right click -> Run ‘ArticleEditInteractionTest’ successfully. I met an error when running gradle test.

Do you know what’s wrong with my unit test?

1. ArticleEditInteractionTest

public class ArticleEditInteractionTest {

@Rule
public TestUiEnvironment environment =
        new TestUiEnvironment(BlogWebTestContainer.Common.INSTANCE).withUserLogin("admin");

private Article article;

@Before
public void setUp() throws Exception {

    TestServiceProxy.mock(NumberIdService.class, new NumberIdService() {
        @Override
        public Long createLongId(String entityName, String sequenceName) {
            return new Long(1);
        }

        @Override
        public Long createCachedLongId(String entityName, String sequenceName) {
            return new Long(1);
        }
    });

    TestEntityFactory<Article> articleFactory =
            environment.getContainer().getEntityFactory(Article.class, TestEntityState.NEW);
    article = articleFactory.create(Collections.emptyMap());
}

@Test
public void testGenerateName() {
    Screens screens = environment.getScreens();
    screens.create(MainScreen.class, OpenMode.ROOT);
    ArticleEdit articleEdit = screens.create(ArticleEdit.class);
    articleEdit.setEntityToEdit(article);
    articleEdit.show();
    assertNull(articleEdit.getEditedEntity().getTitle());
    TextArea content = (TextArea) articleEdit.getWindow().getComponent("contentField");
    articleEdit.onContentFieldTextChange(new TextInputField.TextChangeEvent(content, "Test", 0));

    Button btn = (Button) articleEdit.getWindow().getComponent("popupMessageButton");
    articleEdit.popupMessage(new Button.ClickEvent(btn));
    articleEdit.getEditedEntity().setTitle("Title1");
    articleEdit.getEditedEntity().setContent("Hellow");
    articleEdit.closeWithCommit();
}

}


2. gradle test error.

com.haulmont.cuba.gui.NoSuchScreenException: Screen ‘main’ is not defined. Make sure the screen controller is located inside or below the base package specified in gui:screens element of web-spring.xml
at com.haulmont.cuba.gui.config.WindowConfig.getWindowInfo(WindowConfig.java:560)
at com.haulmont.cuba.web.sys.WebScreens.create(WebScreens.java:173)
at com.haulmont.cuba.gui.Screens.create(Screens.java:70)
at com.haulmont.cuba.web.App.createTopLevelWindow(App.java:300)
at com.haulmont.cuba.web.AppUI.setupUI(AppUI.java:479)
at com.haulmont.cuba.web.AppUI.init(AppUI.java:337)
at com.haulmont.cuba.web.AppTestUtils.initUi(AppTestUtils.java:51)
at com.haulmont.cuba.web.testsupport.TestUiEnvironment.setupVaadinUi(TestUiEnvironment.java:227)
at com.haulmont.cuba.web.testsupport.TestUiEnvironment.setupEnvironment(TestUiEnvironment.java:152)
at com.haulmont.cuba.web.testsupport.TestUiEnvironment.before(TestUiEnvironment.java:131)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:745)

Screen ‘main’ is not defined. Make sure the screen controller is located inside or below the base package specified in gui:screens element of web-spring.xml
com.haulmont.cuba.gui.NoSuchScreenException: Screen ‘main’ is not defined. Make sure the screen controller is located inside or below the base package specified in gui:screens element of web-spring.xml
at com.haulmont.cuba.gui.config.WindowConfig.getWindowInfo(WindowConfig.java:560)
at com.haulmont.cuba.web.sys.WebScreens.getScreenInfo(WebScreens.java:1044)
at com.haulmont.cuba.web.sys.WebScreens.create(WebScreens.java:161)
at com.haulmont.cuba.gui.Screens.create(Screens.java:60)
at test.web.article.ArticleEditInteractionTest.testGenerateName(ArticleEditInteractionTest.java:56)
at java.lang.Thread.run(Thread.java:745)


Thanks! I have figured out where the problem is.

I have two test classes using the codes below.


    Screens screens = environment.getScreens();
    screens.create(MainScreen.class, OpenMode.ROOT);

Through debug, I can see that the first test class is passed when it runs to “screens.create(MainScreen.class, OpenMode.ROOT);”, but the second class doesn’t, it throws an exception.

It seems that the code “screens.create(MainScreen.class, OpenMode.ROOT);” can run once in a JVM, and that explains why it works if I run test class one by one and fails when I run gradle test command, for when I run gradle test, two test classes run in one JVM.

I merged the test codes into one test class, it proves me correct! It works!!!

1 Like