Eric
Eric

Reputation: 125

Can't figure out why mock method doesn't work

I have a mock method

def getBlacklist(newList: List[String]) ={
    when(service.getMyBlacklist).thenReturn(newList)
  }

when I call it in test section, the return value is null

val res = mocks.getBlacklist(List("abcd"))

the main function, can someone help me with that? Thanks.

if (!services.getMyBlacklist.contains(s"$accountId:$ruleName")) {
                ***
   } else {
       ***
  }

Upvotes: 0

Views: 277

Answers (1)

Anand Sai
Anand Sai

Reputation: 1586

I think the issue is mocking is not done correctly. Essentially, what you do is to mock the behaviour of the class such that whenever a method of that class is called in some other class which you are testing, a mocked value is returned. Suppose that services is an instance of Service class as shown below:

class Service() {
  def getBlacklist(): List[String]) = {// Some Original code here}
} 

Now, for the test, use:

val service = mock[Service]
val mockList = List("abc") // change according to your use case 
when(service.getBlacklist).thenReturn(mockList)

Let me know if it helps!!

Upvotes: 1

Related Questions