I am trying to write a Scala unit test using Mockito, for the "doSomeBusinessLogic" method found in the below class:
@Service
public class HotelsService {
private final HotelsServiceInt hotelsServiceInt;
public HotelsService(HotelsServiceInt hotelsServiceInt) {
this.hotelsServiceInt = hotelsServiceInt;
}
public List<String> getHotels(long val1, long val2) {
return hotelsServiceInt.getHotels(val1, val2)
}
public boolean doSomeBusinessLogic(long val1, long val2) {
List<String> hotels = getHotels(val1, val2);
// Do some logic and return true or false
}
And here is the Interface:
public interface HotelsServiceInt {
@GET("/{val1}/{val2}")
Observable<List<String>> getHotels(
@Path("val1") long val1,
@Path("val2") long val2);
}
Here is the Scala test, I wrote for it:
import org.mockito._
import org.scalamock.scalatest.MockFactory
import org.scalatest.{BeforeAndAfter, FunSpec}
import org.mockito.Mockito._
class Spec extends FunSpec with MockFactory with BeforeAndAfter {
@InjectMocks
var hotelsServiceInjectedMock: HotelsService = _
@Mock
var hotelsService: HotelsService = _
before{
hotelsServiceInjectedMock = new HotelsService(hotelsServiceInt)
MockitoAnnotations.initMocks(this)
}
describe("Test") {
it("Should return false.") {
val val1 = 1l
val val2 = 2l
list = new ArrayList()
list.add("Hitlon")
list.add("Sheraton")
list.add("Rotana")
doReturn(list).when(hotelsService).getHotels(
Matchers.any(), Matchers.any())
val result = hotelsServiceInjectedMock.doSomeBusinessLogic(val1, val2)
assert(result == false)
}}
}
However, debugging the test above, shows that List<String> hotels = getHotels(val1, val2);
is getting actually called.
Is there a way have the "doSomeBusinessLogic" code executed, yet to mock the result of the "getHotels" method?