scalamock

Mocking SparkSession for unit testing

本小妞迷上赌 提交于 2021-02-07 08:15:24
问题 I have a method in my spark application that loads the data from a MySQL database. the method looks something like this. trait DataManager { val session: SparkSession def loadFromDatabase(input: Input): DataFrame = { session.read.jdbc(input.jdbcUrl, s"(${input.selectQuery}) T0", input.columnName, 0L, input.maxId, input.parallelism, input.connectionProperties) } } The method does nothing else other than executing jdbc method and loads data from the database. How can I test this method? The

Mocking SparkSession for unit testing

余生颓废 提交于 2021-02-07 08:14:03
问题 I have a method in my spark application that loads the data from a MySQL database. the method looks something like this. trait DataManager { val session: SparkSession def loadFromDatabase(input: Input): DataFrame = { session.read.jdbc(input.jdbcUrl, s"(${input.selectQuery}) T0", input.columnName, 0L, input.maxId, input.parallelism, input.connectionProperties) } } The method does nothing else other than executing jdbc method and loads data from the database. How can I test this method? The

How to call real method on a stub

送分小仙女□ 提交于 2021-01-29 04:41:47
问题 Is there a way to call a real method on a stubbed object with scalamock? I would like to be able to do something like this: class MySpec extends FunSpec with Matchers with MockFactory { trait MyTrait { def f1: Int def f2: Int = f1 } describe("my feature") { it("should work") { val t = stub[MyTrait] (t.f1 _).when().returns(15) // I would like to do the following: // (t.f2 _).when().callRealMethod() t.f2 should be (15) } } } Note: I was able to work around the issue by making f2 final but I

How to call real method on a stub

旧城冷巷雨未停 提交于 2021-01-29 04:37:33
问题 Is there a way to call a real method on a stubbed object with scalamock? I would like to be able to do something like this: class MySpec extends FunSpec with Matchers with MockFactory { trait MyTrait { def f1: Int def f2: Int = f1 } describe("my feature") { it("should work") { val t = stub[MyTrait] (t.f1 _).when().returns(15) // I would like to do the following: // (t.f2 _).when().callRealMethod() t.f2 should be (15) } } } Note: I was able to work around the issue by making f2 final but I

Mock partially a class with scalamock

风流意气都作罢 提交于 2021-01-27 06:31:58
问题 I'm trying to test a class Cls with two functions: A and B . A loads a DataFrame and B calls A then does some operations and returns a new DataFrame . For the sake of example: class Cls { def A(dummy: Int): Int = 5 def B(): Int = A(7) + 1 } With Scalamock how can write my test code ? I tried: test("test case") { val f = stub[Cls] f.A _ when 7 returns 5 assert(f.B() == 6) } I expect test passed successfully and I get 0 did not equal 6 (mytestcase.scala:24) (I do understand that that scalamock

Using a mocked object as an implicit in scala using scalamock

孤街浪徒 提交于 2020-01-16 04:04:31
问题 I am using a trait definition with various concrete derivatives and implicit to inject dependancies into objects and also to mock out parts of the system when unit testing. The problem is that when a mocked version of a type is used as an implicit declaration, it is not matched by scala in the consuming object. Here is a simplified version of my setup. Is there a way to make Test1 work using a mock. Test2 works fine but it hard to maintain and requires too much setup. A model: case class User

Using a mocked object as an implicit in scala using scalamock

爷,独闯天下 提交于 2020-01-16 04:03:06
问题 I am using a trait definition with various concrete derivatives and implicit to inject dependancies into objects and also to mock out parts of the system when unit testing. The problem is that when a mocked version of a type is used as an implicit declaration, it is not matched by scala in the consuming object. Here is a simplified version of my setup. Is there a way to make Test1 work using a mock. Test2 works fine but it hard to maintain and requires too much setup. A model: case class User

Scalamock: How to get “expects” for Proxy mocks?

ぐ巨炮叔叔 提交于 2019-12-23 12:26:55
问题 I am using Scalamock with ScalaTest, and am trying to mock a Java interface. I currently have: private val _iface = mock [MyInterface] now I want to do _iface expects `someMethod returning "foo" once But the compiler does not find expects . I imported org.scalatest._ and org.scalamock.scalatest._ . What else am I missing? 回答1: First of all, proxy mocks are not supported very well in ScalaMock 3, and I think they will be completely removed in ScalaMock 4. Do you really need to use proxy mocks

Unable to create stub with Array argument in ScalMock

a 夏天 提交于 2019-12-12 04:49:40
问题 Here is an example of what I try to achieve. Stub always retuns null, but if I change Array(1L) to * it works. It seems there is a problem with array arguments. trait Repo { def getState(IDs: Array[Long]): String } "test" should "pass" in { val repo = stub[Repo] (repo.getState _).when(Array(1L)).returns("OK") val result = repo.getState(Array(1L)) assert(result == "OK") } 回答1: See this post: Why doesn't Array's == function return true for Array(1,2) == Array(1,2)? ScalaMock is working fine,

Unit testing class that extends a trait - how do I mock and stub methods in the trait?

前提是你 提交于 2019-12-11 10:27:34
问题 I'm using scalatest to unit test a class that extends a trait (in the sense that my class is using the trait as a mixin). The trait contains methods that are helper methods (which ultimately call a few lookups in a database) which I would like to stub out so that I can isolate just the functionality of my class. But I havent been able to find a framework like Mockito or ScalaMock that makes that possible. Question: Can it be achieved with a mocking framework, and if so how? And if not, I'd be