scalatest

How do I test an Akka actor that sends a message to another actor?

我的未来我决定 提交于 2019-11-29 14:42:06
问题 I'm using ScalaTest with the Akka TestKit to write unit and integration tests for an actor I've coded to simply send a message to another actor without mutating any internal state. Take this for example: class MyActor extends Actor { val anotherActor = context.actorOf(Props[AnotherActor]) def receive: Receive = { case MyMessage => anotherActor ! AnotherMessage } } I want to write a test that confirms that anotherActor processed AnotherMessage as a consequence of MyActor processing MyMessage .

Handling Doubles in ScalaTest

半城伤御伤魂 提交于 2019-11-29 13:50:51
I have just started using ScalaTest and I am using the following to compare two Doubles in my spec as follows: it should "calculate the price" in { val x = new X(10,10,12,1000) assert(x.price() === 185.92) } The spec is passing even though I have put in a wrong value of 185.92 to compare against what the price function is returning (that actually returns 10.23 for the case above). I have other specs where I just compare Ints and they work as expected. But the ones involving Doubles are passing regardless. Is there another functions besides assert I should be using to compare Doubles ? EDIT:

Dynamic code evaluation in scala

[亡魂溺海] 提交于 2019-11-29 11:17:12
What is the best way to inject a snippet of code to scala? something like eval in javascript and GroovyScriptEngine. I want to keep my rules/computations/formulas outside the actual data processing class. I have close to 100+ formulas to be executed. The data flow is same for all only the formulas change. What is the best way to do it in scala? and the number of formulas will grow over time. You could use either scala-lang API for that or twitter-eval . Here is the snippet of a simple use case of scala-lang import scala.tools.nsc.Settings import scala.tools.nsc.interpreter.IMain object

How to mock a function within Scala object using Mockito?

泄露秘密 提交于 2019-11-29 10:48:51
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 } }

How to do an instanceof check with Scala(Test)

孤人 提交于 2019-11-29 02:47:18
I'm trying to incorporate ScalaTest into my Java project; replacing all JUnit tests with ScalaTests. At one point, I want to check if Guice's Injector injects the correct type. In Java, I have a test like this: public class InjectorBehaviour { @Test public void shouldInjectCorrectTypes() { Injector injector = Guice.createInjector(new ModuleImpl()); House house = injector.getInstance(House.class); assertTrue(house.door() instanceof WoodenDoor); assertTrue(house.window() instanceof BambooWindow); assertTrue(house.roof() instanceof SlateRoof); } } But I have a problem doing the same with

Mockito matchers, scala value class and NullPointerException

痴心易碎 提交于 2019-11-29 01:13:51
I'm using mockito with scalatest. I have following problem when using matcher with value class. import org.scalatest.FlatSpec import org.scalatest.mock.MockitoSugar import org.mockito.BDDMockito._ import org.mockito.Matchers.any case class FirstId(val value: String) extends AnyVal case class SecondId(val value: String) extends AnyVal trait MockedClass { def someMethods(firstId: FirstId, secondId: SecondId): Int } class ValueClassSpec() extends FlatSpec with MockitoSugar { val mockedClass = mock[MockedClass] val secondId = SecondId("secondId") "Matchers" should "work for value class" in { //

How do I integrate ScalaTest with Spring

冷暖自知 提交于 2019-11-28 23:01:22
问题 I need to populate my ScalaTest tests with @Autowired fields from a Spring context, but most Scalatest tests (eg FeatureSpec s can't be run by the SpringJUnit4ClassRunner.class - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="myPackage.UnitTestSpringConfiguration", loader=AnnotationConfigContextLoader.class) public class AdminLoginTest { @Autowired private WebApplication app; @Autowired private SiteDAO siteDAO; (Java, but you get the gist). How do I populate

How to mock a function within Scala object using Mockito?

自作多情 提交于 2019-11-28 03:30:51
问题 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

How to do an instanceof check with Scala(Test)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 17:19:17
问题 I'm trying to incorporate ScalaTest into my Java project; replacing all JUnit tests with ScalaTests. At one point, I want to check if Guice's Injector injects the correct type. In Java, I have a test like this: public class InjectorBehaviour { @Test public void shouldInjectCorrectTypes() { Injector injector = Guice.createInjector(new ModuleImpl()); House house = injector.getInstance(House.class); assertTrue(house.door() instanceof WoodenDoor); assertTrue(house.window() instanceof BambooWindow

ScalaTest in sbt: is there a way to run a single test without tags?

左心房为你撑大大i 提交于 2019-11-27 16:50:58
I know that a single test can be ran by running, in sbt, testOnly *class -- -n Tag Is there a way of telling sbt/scalatest to run a single test without tags? For example: testOnly *class -- -X 2 it would mean "run the second test in the class. Whatever it is". We have a bunch of tests and no one bothered to tag them, so is there a way to run a single test without it having a tag? This is now supported (since ScalaTest 2.1.3) with: testOnly *MySuite -- -z foo to run only the tests whose name includes the substring "foo". For exact match rather than substring, use -t instead of -z . I wanted to