Synesso
Synesso

Reputation: 38988

Type matching unparameterised Java List in Scala

I'm trying to set a mock expectation in a test in Scala. The mock is on the Hibernate Query object. It has the method:

List list() throws HibernateException;

The List is not parameterised.

When I try to mock this I can't get the types right. E.g.

when(query.list).thenReturn(new ArrayList)
when(query.list).thenReturn(new ArrayList[Any])
// and other variations

Report:

overloaded method value thenReturn with alternatives: 
(java.util.List[?0],<repeated...>[java.util.List[?0]])org.mockito.stubbing.OngoingStubbing[java.util.List[?0]] <and> 
(java.util.List[?0])org.mockito.stubbing.OngoingStubbing[java.util.List[?0]] 
cannot be applied to (java.util.ArrayList[java.lang.Object])

What should my Scala mock expectation look like?

Upvotes: 0

Views: 472

Answers (1)

Eric
Eric

Reputation: 15557

You can use an asInstanceOf cast and write:

when(query.list.asInstanceOf[ArrayList[Any]]).thenReturn(new ArrayList[Any])

Upvotes: 2

Related Questions