Testing GWT JUnit

I am trying very JUnit Testing for GWT if it does not work

public class CheckTest extends TestCase { private ServiceAsync RPC; private HandlerManager EventBus; private CreateTopicPresenter.Display CTV; private CreateTopicPresenter CTP; protected void setUp() { RPC= createStrictMock(ServiceAsync.class); EventBus = new HandlerManager(null); CTV= createStrictMock(CreateTopicView.class); CTP= new CreateTopicPresenter(CTV,RPC,EventBus); } public void testCheck() { CTP.View.getFirstMessage().setValue("MessageTest"); assertTrue(CTP.View.getFirstMessage().getValue().equals("MessageTest")); } 

Stack trace:

  java.lang.ExceptionInInitializerError at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at net.sf.cglib.proxy.Enhancer.setCallbacksHelper(Enhancer.java:619) at net.sf.cglib.proxy.Enhancer.setThreadCallbacks(Enhancer.java:612) at net.sf.cglib.proxy.Enhancer.registerCallbacks(Enhancer.java:581) at org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:194) at org.easymock.internal.MocksControl.createMock(MocksControl.java:60) at org.easymock.EasyMock.createStrictMock(EasyMock.java:70) at com.BiddingSystem.client.CheckTest.setUp(CheckTest.java:26) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) 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) Caused by: java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in client code! It cannot be called, for example, from server code. If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor. at com.google.gwt.core.client.GWT.create(GWT.java:91) at com.google.gwt.user.client.ui.UIObject.<clinit>(UIObject.java:188) ... 24 more 
+4
source share
3 answers

You need to extend the base type of GWTTestCase in order to check GWT runtime-dependent code (for example, calls to the GWT.create () or JSNI methods). Please note that GWTTestCase must also be compatible with GWT, so this will limit the choice of test libraries.

Ideally, most of your application code can be verified using "clean" test applications, with only those verified by the client being verified by GWTTestCases, since the latter have higher overhead.

http://www.gwtproject.org/articles/testing_methodologies_using_gwt.html

+6
source

The reason your code does not work is because the standard jUnit test runner does not handle GWT runtimes as stated in BobV's answer. Its solution may be the easiest to fail, but if you use other frameworks like Mockito, this might not be the easiest solution. There are also some oddities that I discovered with the GWTTestCase extension.

The way I prefer this is to lead the class using the @RunWith([SomeTestRunner].class) annotation @RunWith([SomeTestRunner].class) . For example, GWT Mockito comes with its own test runner, so using it is much more simplified than the GWTTestCase extension, as it allows you to not need to rewrite any parent class that can extend your test case.

+2
source

In some cases, calling GWTMockUtilities.disarm () at the beginning of a test that replaces the normal behavior of GWT.create () with a method that returns null instead of throwing an exception at runtime by writing a clean JUnit test (not GWTTestCase) for the GWT code.

This approach can only be useful in situations where the class under the test does not require an object created using GWT.create ()

0
source

Source: https://habr.com/ru/post/1341586/


All Articles