freepascal / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Method that whose return type erases to Object cannot be used with smart nulls #107

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create an interface with a method whose return type will erase to
Object, like <T> T get(T key).
2. Mock this interface with smart nulls turned on.
3. Upon trying to stub the method, an exception will be thrown:

java.lang.ClassCastException:
org.mockito.internal.creation.jmock.ClassImposterizer$ClassWithSuperclassToWorkA
roundCglibBug$$EnhancerByMockitoWithCGLIB$$be1ca85
cannot be cast to java.lang.Integer
    at
rosetta_stone.mockito_bug.FailsUsingSmartNullsTest.testUsingSmartNulls(FailsUsin
gSmartNullsTest.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
    at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadi
e.java:87)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
    at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.
java:88)
    at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:5
1)
    at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReferen
ce.java:45)
    at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:460)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:673)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java
:386)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.jav
a:196)

What is the expected output? What do you see instead?
 We expect the method to be stubbable as normal.

What version of the product are you using? On what operating system?
 Mockito 1.7, windows

Please provide any additional information below.
 1. Can be worked around by not using smart nulls.
 2. Can be worked around by or by casting the variant parameter to an
Object (see test case #2 in attached project):
  when(mock.get((Object)"a string")).thenReturn((Object)"another string")

Original issue reported on code.google.com by poisonp...@gmail.com on 20 Jul 2009 at 10:03

Attachments:

GoogleCodeExporter commented 9 years ago
A more general workaround is to cast the return value of the mocked method, as 
in...
 when((Object)mock.get("a string")).thenReturn("another string")

This appears to avoid the cast-of-death.

Original comment by poisonp...@gmail.com on 20 Jul 2009 at 10:12

GoogleCodeExporter commented 9 years ago
Damn, I didn't see this issue. I tried your example with latest version of 
Mockito
and it works. I think I'm going to close it...

Let me know if there are any other issues.

Original comment by szcze...@gmail.com on 20 Oct 2009 at 7:35

GoogleCodeExporter commented 9 years ago
I'm still getting the same error with Mockito 1.8.0. Here's my test procedure:

Windows XP SP3, jdk 1.6.0_15-b03
1. Download the zip attached to this ticket. Unzip it. Run cmd and cd to the 
unzipped
directory.
2. Run mvn test. One test fails.
testGetUsingSmartNulls(mockito.erasurebug.FailsUsingSmartNullsTest)
3. Update the pom to mockito-all 1.8.0.
4. Run mvn test again. Still, the same test fails.

Original comment by poisonp...@gmail.com on 22 Oct 2009 at 2:19

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 22 Oct 2009 at 2:43

GoogleCodeExporter commented 9 years ago
will try it. Thanks for reporting!

Original comment by szcze...@gmail.com on 22 Oct 2009 at 2:44

GoogleCodeExporter commented 9 years ago
I tried the zip and it 'mvn test' fails indeed but 'mvn clean test' works fine 
with
version 1.7+

Are you sure this issue still persists?

Original comment by szcze...@gmail.com on 28 Oct 2009 at 10:08

GoogleCodeExporter commented 9 years ago
Closing this one as it seems to be resolved

Original comment by szcze...@gmail.com on 5 Nov 2009 at 10:40

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 11 Nov 2009 at 1:33

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 11 Nov 2009 at 2:36

GoogleCodeExporter commented 9 years ago
Hi... I see this with 1.8.5 but only when the .class files are compiled by 
eclipse.  When I compile using 'javac' I do NOT see the problem. I have no idea 
whether this is an eclipse issue or a mockito issue.  It is, however, very sad.

# OS (Fedora Core 12)
uname -a
Linux linux-dev71.sdsp.mc.xerox.com 2.6.32.26-175.fc12.x86_64 #1 SMP Wed Dec 1 
21:39:34 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux

# eclipse (32 bit, Helios)

# java (32 bit version)
$ java -version
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Server VM (build 19.1-b02, mixed mode)

I built the attached test app with the following libraries:

Hamcrest: hamcrest-all-1.3.0RC2.jar
Mockito: mockito-all-1.8.5.jar
JUnit: 
eclipse-1.3.2.20110301-1815/plugins/org.junit_4.8.1.v4_8_1_v20100427-1100/junit.
jar

Original comment by j...@pentastich.org on 2 May 2011 at 5:51

Attachments:

GoogleCodeExporter commented 9 years ago
The test file I uploaded on comment 10 came in as zero bytes, for some reason.  
Here's another try.

-- Jim

Original comment by j...@pentastich.org on 2 May 2011 at 11:22

Attachments:

GoogleCodeExporter commented 9 years ago
Here are the class files generated by eclipse.

Also, here's the eclipse version info and some info about the project settings.

    Eclipse IDE for Java Developers
    Version: Helios Service Release 2
    Build id: 20110218-0911

The project was created with the default settings:

    JavaSE-1.6
        Compiler compliance level: 1.6
        Generated .class files compatibility: 1.6
        Source compatibility: 1.6

Original comment by j...@pentastich.org on 9 May 2011 at 3:27

Attachments:

GoogleCodeExporter commented 9 years ago
This happens to me as well on eclipse with Mockito 1.9.

List<String> mock = mock(List.class, Mockito.RETURN_SMART_NULLS);
when(mock.get(0)).thenReturn("two");

The second line throws a class cast exception.  I'm using eclipse indigo (3.7).

Original comment by ayac...@gmail.com on 16 May 2012 at 7:02