开发者

Run Current Junit Test in GWTTestCase

I have a JUnit test that I run on one class, but I recently wrote an emulated version for GWT. Since the specification is the same, I would like to use the same test case, but I want it to run in the GWT environment, which would typically be accomplished by extending GWTTestCase.

开发者_运维百科I really want to avoid any copy/paste nonsense, because there are likely to be added tests in the future, which I should not be burdened with copying later.

How can I import/inherit my standard unit test to be run as either a regular test case or a GWT test case?


I have found the solution to this problem.

If you extend the original test with GWTTestCase, you can override getModuleName to return null. This tells GWTTestCase to run as a normal pure java test (no translation at all).

You can then extend this test case with one that overrides getModuleName to return a module name, and the same tests will be run with translation.

Basically:

public class RegularTest extends GWTTestCase {

  @Override
  public String getModuleName() { return null; }

  public void testIt() {...}

}

...and the GWT version...

public class GwtTest extends RegularTest {

  @Override
  public String getModuleName() { return "some.module"; }

}

The downside to this is that it forces you to use JUnit3 style tests, which I find a little annoying, but it beats the alternative.


I think there is no easy way .. But you can extract an interface of your junit test, gwt test case and junit test case implements this interface. You can create a third class for implementation, all test call methods of gwt test case and junit test are delegated to this implementation class.

public interface IRegularTest {
       public void testSomething();
       public void testSomething2();
}

public class RegularTestImpl implements IRegularTest {

      public void testSomething(){
         // actual test code
      }

      public void testSomething2(){
         // actual test code
      }

}

public class RegularTest extends TestCase implements IRegularTest {
      IRegularTest impl = new RegularTestImpl();

      public void testSomething(){
           impl.testSomething   
      }

      public void testSomething2(){
      }
}


public class GwtTest extends TestCase implements IRegularTest {
      IRegularTest impl = new RegularTestImpl();

      public void testSomething(){
           impl.testSomething   
      }

      public void testSomething2(){
      }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜