Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock a function within Scala object using Mockito?

I am really new to Scala. I tried to mock a simple Scala function using Mockito, but I get the following error. I have checked the internet but I was unable to find out the error.

object TempScalaService {
  def login(userName: String, password: String): Boolean = {
    if (userName.equals("root") && password.equals("admin123")) {
      return true
    }
    else return false
  }
}

And my test class is below

class TempScalaServiceTest extends FunSuite with MockitoSugar{

  test ("test login "){
    val service = mock[TempScalaService.type]
    when(service.login("user", "testuser")).thenReturn(true)
    //some implementation
  }
}

But I get the following error:

Cannot mock/spy class     com.pearson.tellurium.analytics.aggregation.TempScalaService$
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class    com.pearson.tellurium.analytics.aggregation.TempScalaService$
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
   at  org.scalatest.mock.MockitoSugar$class.mock(MockitoSugar.scala:74)
    at    com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest.mock(Temp    ScalaServiceTest.scala:7)
at     com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest$$anonfun$    1.apply$mcV$sp(TempScalaServiceTest.scala:10)
    at    com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest$$anonfun$    1.apply(TempScalaServiceTest.scala:9)
    at     com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest$$anonfun$    1.apply(TempScalaServiceTest.scala:9)
    at    org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:    22)
    at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
like image 765
Dilan Avatar asked Jan 15 '17 16:01

Dilan


People also ask

What is Mockito in play Scala?

The framework allows the creation of test double objects (mock objects) in automated unit tests for the purpose of Test-driven Development (TDD). To use Mockito in a Play Scala project follow these easy steps & you are done.

How to create mock objects in Mockito?

As a matter of fact, there are three ways to create mock objects in Mockito: static method org.mockito.Mockito.mock (…) @Mock and calling MockitoAnnotations.initMocks (…) before anything else in @Before method In this article, we’ll be using version 2.0.2-beta which is the latest version as of this writing.

Is it possible to mock a dependency in Scala?

In the Java world, static methods are the bane of mocking. In the Scala world, calls to objects can also be troublesome to deal with for unit tests. But if you follow the code above, you should be able to properly mock out an object based dependency in your class. Great answer! I'm new to Scala and I find it cumbersome.

What mocking frameworks can I use with Scalatest?

Here are links to the mocking frameworks that can be used with ScalaTest: ScalaMock is a native, open-source Scala mocking framework written by Paul Butcher that allows you to mock objects and functions. ScalaMock supports three different mocking styles:


1 Answers

You can define the method in a trait which your object extends. Then simply mock the trait:

trait Login {
  def login(userName: String, password: String): Boolean
}

object TempScalaService extends Login {
   def login(userName: String, password: String): Boolean = {
     if (userName.equals("root") && password.equals("admin123")) {
   return true
   }
    else return false
  }
}

//in your test
val service = mock[Login]
like image 154
Dan Simon Avatar answered Oct 07 '22 07:10

Dan Simon