0

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?

Ababneh A
  • 1,104
  • 4
  • 15
  • 32
  • Would help if you could include the imports in your code, as multiple frameworks use the same names. Specifically, what library is `MockFactory` from, is it ScalaMock? Looks like you're mixing up several mocking frameworks here – Alex Savitsky May 15 '18 at 12:57
  • Added the imports to the test – Ababneh A May 15 '18 at 13:04
  • Ok, first of all, you didn't include **all** of the imports (`MockitoAnnotations`? `@InjectMocks`? `@Mock`?). If you want people to help you, better not expect them to read your mind. Second, you seem to be using several mocking frameworks here. Mocks initialized with one mocking framework would not work when called from another framework – Alex Savitsky May 15 '18 at 13:11

1 Answers1

1

Yes this is achievable because you have a filed:

private final HotelsServiceInt hotelsServiceInt;

You can Mock hotelsServiceInt in your test class and return your mocked results. Before calling doSomeBusinessLogic just return a mocked list and you are good to go. You also need to change the variable types for @Mock and @InjectMock as written below:

   @Mock
   var hotelsServiceInitMock: HotelsServiceInt = _
   @InjectMocks
   var hotelsService: HotelsService = _
   //then do this:
   doReturn(list).when(hotelsServiceInitMock).getHotels(
                 Matchers.any(), Matchers.any())

Now you should be able to get the required result.

Nyle Hassan
  • 188
  • 2
  • 10
  • Won't work! `hotelsServiceInt.getHotels` does not exist. If I do so, the complier would not be able to resolve the reference, giving an error. – Ababneh A May 15 '18 at 12:37
  • You are doing hotelsServiceInt.getHotels in test class? – Nyle Hassan May 15 '18 at 12:39
  • No, I am not doing so. I am just saying that, if I do so, it won't work. – Ababneh A May 15 '18 at 12:40
  • have a look at this: https://stackoverflow.com/questions/41663476/how-to-mock-a-function-within-scala-object-using-mockito – Nyle Hassan May 15 '18 at 12:49
  • Please see the updated answer, I observe you were not mocking the HotelsServiceInt object, it needs to be mocked and @InjectMock will then link this mocked object with the test class. I hope this resolves your query – Nyle Hassan May 16 '18 at 08:24