1 Reply Latest reply on Jul 30, 2013 12:04 PM by jfuerth

    Understanding unit tests in errai

    magick93

      Hi

       

      I'm trying to learn how to write unit tests in gwt and in particular errai.

       

      Ive been studying some examples on the errai github repo, for example https://github.com/errai/errai/tree/4cf20f4d79a06494f022a20adf74a47d4126eebe/errai-ui/src/test/java/org/jboss/errai/ui/test/handler/client.

       

      In my attempt, I am getting the following error. Can anyone help to educate me into how to write tests for errai?

       

      com.google.gwt.junit.JUnitFatalLaunchException: The test class 'app.Test.RuleHeaderTest' was not found in module 'app.Test.RuleHeaderTest'; no compilation unit for that type was seen

                at com.google.gwt.junit.JUnitShell.checkTestClassInCurrentModule(JUnitShell.java:695)

                at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1261)

                at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1228)

                at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:606)

                at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:441)

                at junit.framework.TestCase.runBare(TestCase.java:134)

                at junit.framework.TestResult$1.protect(TestResult.java:110)

                at junit.framework.TestResult.runProtected(TestResult.java:128)

                at junit.framework.TestResult.run(TestResult.java:113)

                at junit.framework.TestCase.run(TestCase.java:124)

                at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:296)

                at junit.framework.TestSuite.runTest(TestSuite.java:243)

                at junit.framework.TestSuite.run(TestSuite.java:238)

                at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)

                at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

                at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

       

      My test is:

       

      package app.Test;
      
      
      import java.util.HashMap;
      import java.util.Map; 
      import org.jboss.errai.enterprise.client.cdi.AbstractErraiCDITest;
      import org.jboss.errai.ioc.client.container.IOC;
      import org.junit.Test; 
      import app.client.local.rule.RuleHeader;
      import app.client.shared.datamodel.Rule;
      import app.client.shared.datamodel.RuleSubType;
      import app.client.shared.datamodel.RuleType; 
      
      public class RuleHeaderTest extends AbstractErraiCDITest {
      
      
                @Override
                  public String getModuleName() {
                    return getClass().getName().replaceAll("client.*$", "app");
                  }
        
                @Test
                public void testRuleHeaderBinding(){
                           final RuleHeaderTestApp app = IOC.getBeanManager().lookupBean(RuleHeaderTestApp.class).getInstance();
                           assertNotNull(app.getComponent());  
        
                          Rule rule = new Rule();
                          rule.setName("new name");
                          rule.setContent("hello world");
        
                          RuleHeader header =new RuleHeader();
                          header.setModel(rule);
      
                } 
      }
      
      

       

       

      package app.Test;
      
      
      import javax.annotation.PostConstruct;
      import javax.inject.Inject; 
      import org.jboss.errai.ioc.client.api.EntryPoint; 
      import app.client.local.rule.RuleHeader; 
      import com.google.gwt.user.client.ui.RootPanel;
      
      @EntryPoint
      public class RuleHeaderTestApp {
        
                @Inject
                private RootPanel root;
        
                @Inject
                RuleHeader component;
        
                public RuleHeader getComponent() {
                          return component;
                }
      
      
                @PostConstruct
                  public void setup() {
                    root.add(component);
                    System.out.println(root.getElement().getInnerHTML());
                  }
      
      }
      
      
        • 1. Re: Understanding unit tests in errai
          jfuerth

          Your GWTTestCase and all of the classes it depends on have to be in the GWT module whose name is returned from the getModuleName() method. Normally you'd set up your testing module the same way as your non-testing modules: put the MyTestModule.gwt.xml in some package, then create subpackages "client" "server" and "shared" under it. Put your test class under the client subpackage. You'll need to configure these source directories with <source path="..."/> entries in your module file. Also, your testing module should import the Errai modules your project normally depends on.

           

          See the GWT docs for a more detailed explanation on GWTTestCase and alternative testing strategies: http://www.gwtproject.org/doc/latest/DevGuideTesting.html

           

          -Jonathan