Scalamock: Mocking call by-name function with arguments - unit-testing

In following code snippet, I need to ensure that BinaryConverter#intToStr is invoked.
import org.scalamock.scalatest.MockFactory
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class Foo {
def foo(x: Int)(b: => String): String = b
}
class BinaryConverter {
def intToStr(x: Int): String = x.toBinaryString
}
class Converter(fooBar: Foo, converter: BinaryConverter) {
def convert2Bin(x: Int): String = fooBar.foo(x)(converter.intToStr(x))
}
class FooTest extends AnyFlatSpec with Matchers with MockFactory {
val mockFoo: Foo = mock[Foo]
val mockBinConverter: BinaryConverter = mock[BinaryConverter]
val converter = new Converter(mockFoo, mockBinConverter)
behavior of "Foo"
it should "mock foo doesn't work" in {
(mockBinConverter.intToStr _).expects(2).returns("Mock 10")
(mockFoo.foo(_: Int)(_: String)).expects(2, *).onCall(_.productElement(1).asInstanceOf[Int => String](2))
converter.convert2Bin(2) shouldBe "Mock 10"
}
}
I've tried to use onCall product but get Converter$$Lambda$132/182531396 cannot be cast to scala.Function1. Although, same code works when casting a parameterless function to Function0, .asInstanceOf[() => String]()

Now, I understand the mistake I was making; I was trying to cast the curried call-by-name argument of the Foo#foo to be the function that was placed in it, in above sample it's BinaryConverter#intToStr(x: Int): String.
Instead, parameter should be cast to () => String and then invoked so the code inside can be executed.
(mockFoo.foo(_: Int)(_: String)).expects(2, *).onCall(_.productElement(1).asInstanceOf[() => String]())

Related

Get private method with reflection in order to pass it to a higher order function in Kotlin

I'm having a hard time trying to get a private method in Kotlin using reflection in order to pass it as a parameter to a higher order function, here is what I got and what I need to do:
The function that gets the private method, probably what I should change or fix:
inline fun <reified T> T.getPrivateFunc(name: String): KFunction<*> {
return T::class.declaredMemberFunctions.first {
it.name == name
}.apply {
isAccessible = true
}
}
This is the high order function I have:
class MyService {
fun myHigherOrderFunction(action: () -> Unit) { /*...*/ }
}
These are the class and the private method I need to get somehow:
class SystemUnderTest {
fun privateFunc() { /*...*/ }
}
Finally a unit test where I I'm trying to make sure the proper method is passed to the high order function, I omitted details for simplification:
// ...
val serviceMock = MyService()
val sut = SystemUnderTest()
// Here is what I'm trying to accomplish
val privateMethod = sut.getPrivateMethod("privateFunc")
service.myHighOrderFunction(privateMethod)
// In the above line I get a compilation error: required () - Unit, found KFunction<*>
service.myHigherOrderFunction(privateMethod as () -> Unit)
// In the above line I get the following runtime error:
// ClassCastException: kotlin.reflect.jvm.internal.KFunctionImpl cannot be cast to kotlin.jvm.functions.Function1
I know the test can be done having the privateFunc as public and maybe annotating it with #VisibleForTesting, but what I want is to avoid compromising the design as long as I can.
Any ideas? Thanks in advance!
I don't think KFunction and KCallable have any notion of a bound receiver, so they are not invokable (have no operator fun invoke), and therefore don't qualify as functions. So I think you have to wrap the KFunction object in a function to be able to pass it to your higher order function. To call a KFunction, you pass the instance of the receiver class as the first argument.
val serviceMock = MyService()
val sut = SystemUnderTest()
val privateMethod = sut.getPrivateMethod("privateFunc")
service.myHighOrderFunction { privateMethod.call(sut) }
Edit: To internalize the creation of the wrapped function, you could do this:
inline fun <reified T> T.getZeroArgPrivateMethod(name: String): () -> Unit = {
T::class.declaredMemberFunctions.first {
it.name == name
}.apply {
isAccessible = true
}.call(this)
}
//...
val serviceMock = MyService()
val sut = SystemUnderTest()
val privateMethod = sut.getZeroArgPrivateMethod("privateFunc")
service.myHighOrderFunction(privateMethod)

Check the input parameters with groovy.mock.interceptor

I'm trying to have some unit tests using groovy.mock.interceptor. I want to assert that a function was indeed called with some specific values as arguments. I can't find how to do that. Any help?
This is what it looks like:
import groovy.mock.interceptor.MockFor
import org.junit.Test
class MyClassTest extends GroovyTestCase {
#Test
void test_correctness_of_passed_arguments() {
def mock = new MockFor(MyClass)
mock.ignore('main')
mock.demand.myFunction{a, b, c -> '0'} // Is this where I should enforce the input params?
mock.use {
def foo = new MyClass()
foo.main() // <--- this is in there that it gets executed
}
mock.expect.verify()
mock.demand.recorded[0] // <--- can I get what has been passed afterwards?
}
}
You can't achieve expected behavior with MockFor class. Ignoring main method has one significant effect - inner method myFunction gets executed, but it happens without a presence of MockInterceptor. You can put a breakpoint in groovy.mock.MockProxyMetaClass class in the beginning of invokeMethod (line 74) and run a debugger to see what happens.
public Object invokeMethod(final Object object, final String methodName, final Object[] arguments) {
if (null == interceptor && !fallingThrough) {
throw new RuntimeException("cannot invoke method '" + methodName + "' without interceptor");
}
Object result = FALL_THROUGH_MARKER;
if (interceptor != null) {
result = interceptor.beforeInvoke(object, methodName, arguments);
}
if (result == FALL_THROUGH_MARKER) {
Interceptor saved = interceptor;
interceptor = null;
boolean savedFallingThrough = fallingThrough;
fallingThrough = true;
result = adaptee.invokeMethod(object, methodName, arguments);
fallingThrough = savedFallingThrough;
interceptor = saved;
}
return result;
}
Invoking foo.main() method in the mock.use {} block invokes this method for non-null interceptor. The result returned by interceptor.beforeInvoke() is equal to FALL_THROUGH_MARKER because main method is marked as ignored. In this case interceptor is set to null temporarily and the method gets invoked in a regular way - it invokes inner myFunction method, but this fact is not recorded due to null interceptor at this point.
Basically, you treat mock object in your test case not as a mock, but rather as a spy object. Groovy standard mocking library does not support spy objects, but you can use e.g. Spock Framework to write tests using spy objects. The test you have shown in the question could look like this using Spock:
import spock.lang.Specification
class ExampleSpec extends Specification {
static class MyClass {
def main() {
return myFunction(0, 0 ,0)
}
def myFunction(def a, def b, def c) {
return '2'
}
}
def "should call myFunction with specific parameters"() {
given:
def foo = Spy(MyClass)
when:
foo.main()
then:
1 * foo.myFunction(0, 0, 0)
and:
0 * foo.myFunction(1,0,0)
}
}
It executes a real foo.main() method, but it mocks foo.myFunction() method and records invocations and tests if the method got invoked with correct parameters - it records that it got invoked once with parameters (0, 0, 0) and that it was not invoked with parameters (1, 0, 0).
IMPORTANT: If you create mock/spy objects from classes and not interfaces, then you need to add cglib-nodep dependency together with Spock.
Okay, this is doable as mock.demand.myFunction takes a normal Closure.
I ended up with something like this:
import groovy.mock.interceptor.MockFor
import org.junit.Test
class MyClassTest extends GroovyTestCase {
#Test
void test_correctness_of_passed_arguments() {
def mock = new MockFor(MyClass)
mock.ignore('main')
def res = []
// the mocked function stores its values in `res` and returns '0'
mock.demand.myFunction(4) {a, b, c ->
res.add([a, b, c])
'0'
}
mock.use {
def foo = new MyClass()
foo.main() // <--- this is in there that it gets executed
}
mock.expect.verify()
res[0] // <--- I can then access the values there
}
}
In the above example, I request myFunction to be called 4 times.

Mock object used insided the to be tested function using spock

I can mock a function of a to be tested class in several ways. But how do I mock an object that is created inside of a to be tested method?
I have this to be tested class
#Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')
import groovyx.net.http.HTTPBuilder
class totest {
def get() {
def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )
return html
}
}
How do I mock http.get so I can test the get function:
class TestTest extends Specification {
def "dummy test"() {
given:
// mock httpbuilder.get to return "hello"
def to_test = new totest()
expect:
to_test.get() == "hello"
}
}
A better approach would be to pass the HTTPBuilder into your constructor and then the test code can pass test mocks instead.
But if you want to mock the class construction going on internal to your code, have a look at mocking constructors and classes using GroovySpy and GroovyMock on here: http://spockframework.org/spock/docs/1.0/interaction_based_testing.html
You would need to do something like the below code:
import spock.lang.Specification
import groovyx.net.http.HTTPBuilder
class totest {
def get() {
def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )
return html
}
}
class TestTest extends Specification{
def "dummy test"() {
given:'A mock for HTTP Builder'
def mockHTTBuilder = Mock(HTTPBuilder)
and:'Spy on the constructor and return the mock object every time'
GroovySpy(HTTPBuilder, global: true)
new HTTPBuilder(_) >> mockHTTBuilder
and:'Create object under test'
def to_test = new totest()
when:'The object is used to get the HTTP result'
def result = to_test.get()
then:'The get method is called once on HTTP Builder'
1 * mockHTTBuilder.get(_) >> { "hello"}
then:'The object under test returns the expected value'
result == 'hello'
}
}
What are you testing here? Do you care how the method gets it's result? Surely you care more that it gets the right result? In that case, the method should be changed so the URL is configurable, then you can stand up a server that returns a known string, and check that string is returned

Is it possible to overload a constructor that accepts a list in scala?

I am trying to overload the constructor to a class so that it can accept lists of two different types of objects:
class myClass(){
var someStrings: List[String]=List[String]()
println("hello!")
def this(strings : List[String])={
this()
this.someStrings=strings
}
def this(ints: List[Int])={
this()
this.someStrings=ints.map(x => x.toString)
}
}
In this case, accept a list of ints or strings, and save a list of strings to the variable someStrings. The code above doesn't work:
error: double definition:
constructor myClass: (strings: List[String])myClass at line 12 and
constructor myClass: (ints: List[Int])myClass at line 17
have same type after erasure: (strings: List)myClass
def this(ints: List[Int])={
^
Is there a better way of doing this in scala? (Other than taking List[Any] and testing the elements)
Thanks!
create functions on a companion object which do the construction for you in a typesafe way that can be checked at compile time:
class myClass(){
var someStrings: List[String]=List[String]()
println("hello!")
}
object myClass {
def fromStrings(strings: List[String]) = {
val c = new myClass
c.someStrings = strings
}
def fromInts(ints: List[Int]) = {
val c = new myClass
c.someStrings = ints.map(_.toString)
}
}
object Usage {
val c1 = myClass.fromStrings(List("a","b","c"))
val c2 = myClass.fromInts(List(1,2,3))
}
I would urge you to avoid overloading in general, or checking types at runtime, when you could be checking types at compile-time instead
That's what DummyImplicit is for:
def this(strings: List[String])={
this()
this.someStrings=strings
}
def this(ints: List[Int])(implicit d: DummyImplicit)={
this()
this.someStrings=ints.map(x => x.toString)
}
This makes the erased signatures of constructors (i.e. the ones JVM sees) MyClass(List) and MyClass(List, DummyImplicit), so the overloading is allowed.
However, as #stew says, it could be a better idea to avoid overloading in this case.
In addition to the other answer, another thing you can do is use Arrays. The type information on an array is kept at runtime, so you can do overloading based on the type parameter.
class myClass() {
var someStrings: Array[String] = Array[String]()
println("hello!")
def this(strings: Array[String]) = {
this()
this.someStrings = strings
}
def this(ints: Array[Int])={
this()
this.someStrings = ints.map(x => x.toString)
}
}
Arrays are much underused in my opinion

specs2: Is there a way to use doNothing while mocking with Mockito?

Let's say that I have a mocked trait Foo:
trait Foo {
def op(x: String): Unit
}
and I mocked this interface using
val mockedFoo = mock[Foo]
I want the method op to throw an exception second time I call it, e.g.
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
trait Foo {
def op(x: String): Unit
}
class DummySpec extends Specification with Mockito {
"dummy" should {
"test" in {
val mockedFoo = mock[Foo]
org.mockito.Mockito.doNothing().doThrow(new RuntimeException).when(mockedFoo).op(any[String])
mockedFoo.op("This one should work fine") should not(throwAn[Exception])
mockedFoo.op("This one should throw an exception") should throwAn[Exception]
}
}
}
Is there a way to do this in specs2 style? e.g.
mockedFoo.op(any[String]) returns Unit thenThrows new RuntimeException
but this doesn't compile.
Thanks!
The Unit return type makes things a bit trickier as you can't just chain:
returns "foo" thenThrows new RuntimeException
But you can still solve this problem if you use answers like below:
mockedFoo.op(anyString) answers {args => } thenThrows new RuntimeException
See if this works for you.