can we mock method inside object - scala

object Abc {
method1(param1,param2):Future[Option[String]] = {
//some work
}
}
We are using method1 somewhere and need to stub method1 how can we do this in scala using mockito
I am doing like this :
when(method1(mockParam1, mockParam2)).thenReturn(SomeExpectedStuff)
but facing NPE .

Related

Mockito-Mock functions within scala object for unit testing

I have a few functions within a scala object. The functions internally call other functions of the same object.
object A {
def method1:Unit= {
spark=CreateSparkSession.create()
Method2(spark,dbnm)
}
def Method2(spark:Sparksession, dbnm:String):Unit= {
//some implementation
}
}
How can I write Unit testcase for Method1 without actually invoking method2.
CreateSparksession is another object with create method that returns sparksession.
You cannot mock methods in an object. And you should not mock methods in class that you are testing (if it looks like you need to, it is a definite symptom of violating the single responsibility principle).
What you can do is something like this:
trait Api1 {
def method1(
...
): Unit // NB: also, should not really return a Unit: how are you going to test this???
}
trait Api2 {
def method2(...): Unit // See above
}
class Impl2 extends Api2 {
def method2(...) = // Do stuff
}
class Impl1(val api2: Api2) extends Api1 {
def method1(...) = { ... ; api2.method2(); ... }
}
// You should not really need this, but, you can have it if you want
object A extends Impl1(new Impl2)
So, now testing this code is trivial:
describe("Impl2") {
it ("does nothing") {
new Impl2().method2("foo")
// Nothing happens
// this is what I meant: how do you know it worked?
}
}
describe("Impl1") {
it ("does nothinig") {
val a2 = mock[Api2]
doNothing when a2 method2(any)
val fixture = new Impl1(a2)
fixture.method1()
// Again, nothing happens!
verify(a2).nothing("foo")
}

Use #ClassRule in Kotlin

In JUnit you can use #ClassRule to annotate an static field.
How can I do this in Kotlin?
I tried:
object companion {
#ClassRule #JvmStatic
val managedMongoDb = ...
}
and
object companion {
#ClassRule #JvmField
val managedMongoDb = ...
}
but none of the last works because rule isn't executed.
I double checked that exactly same rule works fine without static context:
#Rule #JvmField
val managedMongoDb = ...
You are not using companion objects correctly. You are declaring an object (single instance of a class) called companion instead of creating a companion object inside of a class. And therefore the static fields are not created correctly.
class TestClass {
companion object { ... }
}
Is very different than:
class TestClass {
object companion { ... } // this is an object declaration, not a companion object
}
Although both are valid code.
Here is a correct working example of using #ClassRule, tested in Kotlin 1.0.0:
class TestWithRule {
companion object {
#ClassRule #JvmField
val resource: ExternalResource = object : ExternalResource() {
override fun before() {
println("ClassRule Before")
}
override fun after() {
println("ClassRule After")
}
}
}
#Test fun testSomething() {
println("Testing...")
}
}
This outputs:
ClassRule Before
Testing...
ClassRule After

Calling scala method with a gatling scenario

I have a simple function like :
object Actions {
def myAction() {
//do some stuff
}
}
Is there a way to use this method as a scenario ? Like :
val scn = scenario("My scenario").exec(Actions.myAction())
This code doesn't compile but I don't understand why.. it seams exec is waiting for a chain / scenario builder (or a validation object).
Thks !
Yes, exec expects a ChainBuilder, but here, you're returning nothing (Scala deprecated procedure style) because you're missing the = sign.
object Actions {
def myAction() = {
//do some stuff
}
}

Is this apply method defined correctly?

When I run below code the apply method is not being called :
Java :
public class Driver {
public static void main(String args[]){
new parallel.TestData();
}
}
Scala :
package parallel
class TestData {
def apply() = {
println("in apply method ")
}
}
If I use :
new parallel.TestData().apply();
then the apply method is called correctly but should the apply() method not be implicitly invoked from above code ? Is the apply method not implemented correctly ?
In Scala, there is a difference between a constructor and the apply method. It seems that you expect the apply method to act like a constructor. However, the constructor is a method named def this(...). Your are hence not defining a constructor.
The apply method can be called by using a name just like any other method (.apply()) or by using parantheses (()). After having created a new instance of your class, you still need to call the apply method.
You can thus either create a new instance of your class and call apply:
new parallel.TestData()()
// ^ ^---- apply method
// |------- constructor
Or you can implement an object instead of a class:
object TestData {
def apply() = {
println("in apply method ")
}
}
parallel.TestData()
Update: When calling Scala code from Java, you would have to stick to the first method. As Java does not add syntactic sugar for the apply method, you are left with the option of an explicit call: new parallel.TestData().apply().

How to mock/stub a method local dependency with Mockito?

Let's say that I have this:
class Dependency1 {
def methodD1 { }
}
class Dependency2 {
val dependency1 = new Dependency1
}
def myMethod() {
val a = new Dependency1
// I want to be able to stub this
val b = a.dependency1.methodD1()
...
}
I want to do something like in RR (ruby mock library):
any_instance_of(Dependency1) do | obj |
stub(obj) { "123" } # this would be like stub(obj) toReturn("123") with Mockito in Scala
end
I know that there is an any method in Mockito but it's a matcher. I'm looking for something like:
stub(anyInstanceOf(Dependency1).methodD1) toReturn("123")
Is there a way to mock/stub a local dependency with Mockito/EasyMock/PowerMock/JMock ?
I'm using ScalaTest with MockitoSugar.
I know this is Scala, not Java; but if you have a look at the Mockito wiki article at http://code.google.com/p/mockito/wiki/MockingObjectCreation, it describes how to solve this in Java. I imagine the Scala solution will be much the same.