returning right "answer" needed now?

11 views
Skip to first unread message

rogerdpack

unread,
Mar 15, 2016, 7:00:51 PM3/15/16
to mockito
As a note, with the latest beta and JDK 8:

this method "getLong" returns a long.  If i mock it out like this,



    Mockito.when(dbConn.getLong()).thenAnswer(new Answer<Object>() {
      @Override
      public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
        return Math.random() * Long.MAX_VALUE; // returns a double
      }
    });


 I get a classcast exception
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long

at DBConn$MockitoMock$789232147.getLong(Unknown Source)

if I return the "right" type (a long) it works, though somehow this used to not be needed (jdk 7)?  Maybe it's expected, though I'd mention it anyway.


    Mockito.when(dbConn.getLong()).thenAnswer(new Answer<Object>() {
      @Override
      public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
        return (long) (Math.random() * Long.MAX_VALUE); // return a long
      }
    });


Cheers!

Eric Lefevre-Ardant

unread,
Mar 16, 2016, 9:57:12 AM3/16/16
to moc...@googlegroups.com
Well, Math.random() returns a double. When you multiply a double with a long in Java, you get a double. AFAIK, this has always been the case, including in Java 7. 

Of course, using an Answer here is confusing because the returned type is an Object, so you get the error message later.

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/415685e9-9a2a-4aa6-9f02-fadd2693e167%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roger Pack

unread,
Mar 16, 2016, 11:12:57 AM3/16/16
to moc...@googlegroups.com
On Wed, Mar 16, 2016 at 7:56 AM, Eric Lefevre-Ardant
<er...@ericlefevre.net> wrote:
> Well, Math.random() returns a double. When you multiply a double with a long
> in Java, you get a double. AFAIK, this has always been the case, including
> in Java 7.
>
> Of course, using an Answer here is confusing because the returned type is an
> Object, so you get the error message later.

Yeah, the error message is good enough. I just thought I'd point it
out in case it was a regression (since "some time ago" this did work,
not sure how...)
Cheers!
-roger-
> You received this message because you are subscribed to a topic in the
> Google Groups "mockito" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/mockito/dzNhL0xch7k/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> mockito+u...@googlegroups.com.
> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mockito.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mockito/CAPffK0HnHY9hM%3DFC00nHBxWR-52hKfv5rD%2BYRAU-m6auBxQWLg%40mail.gmail.com.

Francisco Olarte

unread,
Mar 16, 2016, 2:45:19 PM3/16/16
to moc...@googlegroups.com
On Wed, Mar 16, 2016 at 4:12 PM, Roger Pack <rogerp...@gmail.com> wrote:
> Yeah, the error message is good enough. I just thought I'd point it
> out in case it was a regression (since "some time ago" this did work,
> not sure how...)

Bear in mind correct code has to do the right thing 100% of the time,
but incorrect code does not have to fail 100% of the time. You may
have been hitting dome undefined behaviour which has changed.

Francisco Olarte.
Reply all
Reply to author
Forward
0 new messages