question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassCastException: cannot be cast to com.google.gwt.dom.client.AnchorElement

See original GitHub issue

I am new to gwtmockito and I really like it. But here is an issue that caught me.

A simplified class is like this // a class needs to be tested public class MyClass { Anchor obj;

public MyClass(String userName) {
        //this will fail with GwtMockito
        obj= new Anchor();
}

}

the class to test it is: //a test class @RunWith(GwtMockitoTestRunner.class) public class MyClass_Test { MyClass app;

@Before
public void setUp() {
    //this will fail with GwtMockito
    app = new MyClass("");
}

@Test
public void test1() {
    Assert.assertTrue(true);
}

}

however, when I run this test code, I get an error like this

java.lang.ClassCastException: com.google.gwt.user.client.Element$$EnhancerByMockitoWithCGLIB$$86942b16 cannot be cast to com.google.gwt.dom.client.AnchorElement at com.google.gwt.dom.client.AnchorElement.as(AnchorElement.java:34) at com.google.gwt.user.client.ui.Anchor.getAnchorElement(Anchor.java:564) at com.google.gwt.user.client.ui.Anchor.<init>(Anchor.java:119) at com.google.gwt.user.client.ui.Anchor.<init>(Anchor.java:104) at com.sample.failed.MyClass.<init>(MyClass.java:13) at com.sample.failed.MyClass_Test.setUp(MyClass_Test.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 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.RunBefores.evaluate(RunBefores.java:24) 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 com.google.gwtmockito.GwtMockitoTestRunner.run(GwtMockitoTestRunner.java:284) 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)

I am using gwtmockito 1.1.3 and gwt v2.5.1 with eclipse Kepler v4.3 (x64). GWT doesn’t seem to like “new Anchor()” with gwtmockito. can you help me on this??

PS, it seems to make the code quoted automatically but improperly, not sure how to make it right, sorry about that.

thanks,

Edit: This seems very common with GWT classes. Here is another example: java.lang.ClassCastException: com.google.gwt.user.client.Element$$EnhancerByMockitoWithCGLIB$$44131348 cannot be cast to com.google.gwt.dom.client.SelectElement at com.google.gwt.user.client.ui.ListBox.getSelectElement(ListBox.java:600) at com.google.gwt.user.client.ui.ListBox.insertItem(ListBox.java:346) at com.google.gwt.user.client.ui.ListBox.insertItem(ListBox.java:328) at com.google.gwt.user.client.ui.ListBox.insertItem(ListBox.java:295) at com.google.gwt.user.client.ui.ListBox.addItem(ListBox.java:173)

addItem is called by MyClass, but when testing MyClass, this error occurs. can’t GwtMockito handle casting within GWT classes?

Please help… this makes me desperate.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
ekueflercommented, Feb 2, 2015

@krishnasv369, Thanks for pointing that one out. I’ve inserted a fix that will work around the problem in the next version of GwtMockito, which will be released in a week or two. Until then you can either try using the snapshot version or add the following annotation to the test class that’s giving you trouble:

@WithClassesToStub(CheckBox.class)

That should also work around the problem for you.

0reactions
krishnasv369commented, Jan 14, 2015

Hi ekuefler.

i am facing same kind of issue with “RadioButton”.

java.lang.ClassCastException: com.google.gwt.user.client.Element$$EnhancerByMockitoWithCGLIB$$e41b1350 cannot be cast to com.google.gwt.dom.client.InputElement

is there any work around without modifying GWT kit .

and also i dont want to use uibider. since i have committed my huge code already and now i now i cant change to uibinder.

so let me know is there any way of fixing code. thanks in advance .

Read more comments on GitHub >

github_iconTop Results From Across the Web

GWT: Label cannot be cast to com.google.gwt.user.client.ui ...
So I have to cast in the loop, but surprising error appears. It says. ClassCastException: com.google.gwt.user.client.ui.Label cannot be cast ...
Read more >
AnchorElement (GWT Javadoc)
The anchor element. ... Fields inherited from class com.google.gwt.dom.client. ... Determine whether the given Element can be cast to this class.
Read more >
Error java.lang.ClassCastException while creating
ClassCastException : org.apache.xerces.dom.ElementNSImpl cannot be cast to com.apigee.messaging.config.beans.TargetConnection.
Read more >
Uses of Class java.lang.Object (GWT 2.6.1)
com.google.gwt.canvas.dom.client ... Classes used to build DOM elements. ... value cast to the type accepted by the Editor or throws a ClassCastException ....
Read more >
Class Cast Exception while running requestfactory-apt
to Google Web Toolkit ... java/com/foo/cis/foobar/client/requestfactory/Persistable.java:9: ... $TypeSymbol cannot be cast to javax.lang.model.element.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found