scalatest

ScalaTest: Assert exceptions in failed futures (non-blocking)

怎甘沉沦 提交于 2019-11-30 11:15:48
问题 import org.scalatest.{ FlatSpec, Matchers, ParallelTestExecution } import org.scalatest.concurrent.ScalaFutures import org.apache.thrift.TApplicationException class Test extends FlatSpec with Matchers with ScalaFutures with ParallelTestExecution { it should "throw org.apache.thrift.TApplicationException for invalid Ids" in { val future: Future[Response] = ThriftClient.thriftRequest whenReady(future) { res => { intercept[TApplicationException] { } } } } } Question: How do you assert expected

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

限于喜欢 提交于 2019-11-30 09:44:58
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 . The classic example is to use TestActorRef to get at the underlying actor and check for some internal

java.lang.NoClassDefFoundError: scala/Product$class

落花浮王杯 提交于 2019-11-30 06:22:48
I am new to scala and I am trying out few sample codes for testing. However I am facing some issues when I run the test code. When I run the test, I am getting an error [trace] Stack trace suppressed: run last test:executeTests for the full output. [error] (test:executeTests) java.lang.NoClassDefFoundError: scala/Product$class [error] Total time: 3 s, completed Feb 27, 2017 6:57:15 PM My code is as follows FilterChecks.scala class filterChecks extends FlatSpec { "Filter checker passed a filename which is present in directory" should "return file name" in { val matchingFileName = new FileObject

ScalaMock. Mock a class that takes arguments

我是研究僧i 提交于 2019-11-30 05:03:08
问题 Full disclosure: I'm very new to mocking and mocking frameworks. I'm trying to use ScalaMock because it seemed like the 'default' mocking framework to use with ScalaTest but I am happy to use any other framework which is compatible with ScalaTest. The problem: I've written in Scala a class that talks to a socket. The class has a type parameter of what sort of socket it is to talk to and one of it's arguments is a factory for creating sockets of that type. It has the signature: class XScanner

Scala and Mockito with traits

不羁的心 提交于 2019-11-30 04:50:53
问题 I had a simple class that naturally divided into two parts, so I refactored as class Refactored extends PartOne with PartTwo Then the unit tests started failing. Below is an attempt to recreate the problem. The functionality of all three examples is the same, but the third test fails with a NullPointerException as indicated. What it is about the use of traits that is causing the problem with mockito? Edit: Is Mockito the best choice for Scala? Am I using the wrong tools? import org.scalatest

What is the === (triple-equals) operator in Scala Koans?

拜拜、爱过 提交于 2019-11-30 04:10:38
I started working my way through the Scala Koans , which is organized around a suite of unit tests with blanks that one needs to fill in. (This idea was modeled after a similar Ruby Koans project.) You start the sbt tool running a test, and it admonishes: [info] + ***************************************** [info] + [info] + [info] + [info] + Please meditate on koan "None equals None" of suite "AboutEmptyValues" [info] + [info] + [info] + [info] + ***************************************** ...and so you go look at this unit test and it says: test("None equals None") { assert(None === __) } ...and

org.scalatest: Global setup (like beforeAllSuites?)

筅森魡賤 提交于 2019-11-30 02:54:04
问题 I have a scala application with some test using org.scalatest. These test need some global setup (and teardown), in order to manage the test database. Please don't tell me my tests should not hit the database and I should do it the Java-DAO-Stub-WTF-Overkill-Way™ :-). I'm running the tests using SBT, which provides a way to execute code before and after test: testOptions in Test += Tests.Setup( () => println("Setup") ) testOptions in Test += Tests.Cleanup( () => println("Cleanup") )

How do I integrate ScalaTest with Spring

狂风中的少年 提交于 2019-11-30 02:06:31
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 @Autowired fields from an ApplicationContext for ScalaTest? class AdminLoginFeatureTest extends FeatureSpec

In ScalaTest is there any difference between `should`, `can`, `must`

江枫思渺然 提交于 2019-11-30 00:26:39
问题 Just started using ScalaTest and I quite like it. By just reading the docs I have thus far been unable to figure out whether there is any substantial difference between the can , should and must clauses for a FlatSpec . In particular, I'm wondering whether a must failure is treated any differently from a should one - or it's just "syntactic sugar" to make the tests better self-documented. 回答1: should and must are the same semantically . But it's not about better documentation, it's basically

ScalaTest: Assert exceptions in failed futures (non-blocking)

元气小坏坏 提交于 2019-11-30 00:19:42
import org.scalatest.{ FlatSpec, Matchers, ParallelTestExecution } import org.scalatest.concurrent.ScalaFutures import org.apache.thrift.TApplicationException class Test extends FlatSpec with Matchers with ScalaFutures with ParallelTestExecution { it should "throw org.apache.thrift.TApplicationException for invalid Ids" in { val future: Future[Response] = ThriftClient.thriftRequest whenReady(future) { res => { intercept[TApplicationException] { } } } } } Question: How do you assert expected failures in Futures without blocking? The above doesn't work, the exception is thrown before the