Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an overloaded java generic method from scala

Tags:

People also ask

Can you overload methods in Scala?

Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name.

How can generic method be overloaded?

A generic method can also be overloaded by nongeneric methods. When the compiler encounters a method call, it searches for the method declaration that best matches the method name and the argument types specified in the call—an error occurs if two or more overloaded methods both could be considered best ...

Is return type should be same in overloading?

No, you cannot overload a method based on different return type but same argument type and number in java.

What are the rules for method overloading in java?

The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.


I'm using Mockito to mock an object with a method which returns an un-parametrized ArrayList, and I cannot figure out how to get this to work

Method signature to mock

public java.util.ArrayList getX()

Test code

var mockee = mock(classOf[Mockee])
when(mockee.getX).thenReturn(Lists.newArrayList(x): ArrayList[_])

This actually compiles fine in IntelliJ, but at runtime throws:

[error] ....scala:89: overloaded method value thenReturn with alternatives:
[error]   (java.util.ArrayList[?0],<repeated...>[java.util.ArrayList[?0]])org.mockito.stubbing.OngoingStubbing[java.util.ArrayList[?0]] <and>
[error]   (java.util.ArrayList[?0])org.mockito.stubbing.OngoingStubbing[java.util.ArrayList[?0]]
[error]  cannot be applied to (java.util.ArrayList[_$1])
[error]       when(mockee.getX).thenReturn(Lists.newArrayList(x): ArrayList[_])

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!