I would like to return java.util.List
val cacheManager = mock(classOf[CacheManager])
val cache = mock(classOf[Cache])
when(cacheManager.getCache(anyString)).thenReturn(cache)
when(cache.getKeys.asInstanceOf[List[String]]).thenReturn(List("some_key"))
It compiles, but throws:
java.lang.NullPointerException
at net.sf.ehcache.Cache.checkStatus(Cache.java:2722)
at net.sf.ehcache.Cache.getKeys(Cache.java:1912)
at (last line of code)
How can I fix it? I tried a couple of ways, but without a success.
Method signature:
public final java.util.List getKeys() throws java.lang.IllegalStateException, net.sf.ehcache.CacheException { /* compiled code */ }
PowerMock, an extension to Mockito and other, however can mock final methods. http://code.google.com/p/powermock/wiki/MockFinal
Also when referring to java.util.List you probably want to use the full name and not just List, which normally is scala.collection.immutable.List.