how to mock a method that throws an exception - scala

The following method throws an exception if a question is not found
def getQuestionFromQuestionID(questionKey: PracticeQuestionKeys) = {
logger.trace(s"getting question with keys ${questionKey}")
val practiceQuestionFuture: Future[Option[PracticeQuestion]] = findOne(questionKey)
for (questionOption <- practiceQuestionFuture) yield {
questionOption.fold(throw QuestionNotFoundException())(question => {
logger.trace("got question " + question)
question
})
}
}
I call it like the following in a controller.
def function1(){...
val res = for{existingQuestion <- questionsRepository.getQuestionFromQuestionID(questionKey)
res.recover{
case exception =>...
}...}
...}
I am writing a test for function1 simulating that an exception is thrown. I wrote the following but I get dead code error when I compile the code.
when(answerTestEnv.mockPracticeQuestionsRepository.getQuestionFromQuestionID(ArgumentMatchers.any[PracticeQuestionKeys])).thenReturn(
throw QuestionNotFoundException()
)
Error
Error:(92, 9) dead code following this construct
throw QuestionNotFoundException()
I changed the code to
when(answerTestEnv.mockPracticeQuestionsRepository.getQuestionFromQuestionID(ArgumentMatchers.any[PracticeQuestionKeys])).thenThrow(
QuestionNotFoundException()
)
But the test case fails with error
Checked exception is invalid for this method!
Invalid: utilities.QuestionNotFoundException: Question not found
org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: utilities.QuestionNotFoundException: Question not found
How can I simulate the exception scenario?

For methods with return value you can use .thenThrow (I's assuming you are using mockito, because scalamock uses different conventions)
when(
answerTestEnv
.mockPracticeQuestionsRepository
.getQuestionFromQuestionID(ArgumentMatchers.any[PracticeQuestionKeys])
).thenThrow(new QuestionNotFoundException())
and if the method is of type void (Unit) you use doThrow(ex).when(mock).call:
doThrow(new QuestionNotFoundException())
.when(answerTestEnv.mockPracticeQuestionsRepository)
.getQuestionFromQuestionID(ArgumentMatchers.any[PracticeQuestionKeys])

Related

How to fix RuntimeException: error reading Scala signature of com.snp.utils.DbUtils: error reading Scala signature

I have defined
var status:String = null;
var error_msg:String = null;
try{ status ="Error"; error_msg= "n/a";
}
When I place my method in DbUtils object and call from my main class it is throwing error, i.e. when calling
DbUtils.writeToCassandraAuditLog(spark, status, error_msg);
it throws below error
2018-10-23 14:03:37 ERROR java.lang.RuntimeException: error reading Scala signature of com.snp.utils.DbUtils: error reading Scala signature of org.apache.spark.sql.package: assertion failed: unsafe symbol Unstable (child of <none>) in runtime reflection universe
at scala.reflect.internal.pickling.UnPickler.unpickle(UnPickler.scala:46)
But when I place/define the methods in my main class/object it is working fine.
writeToCassandraAuditLog(spark, status, error_msg);
How to fix this issue?

how to test Akka Action with expectMsgClass

I am new to scala akka
in my test code I have this
val sendEmailActor = system.actorOf(SendEmailActor.props)
sendEmailActor ! email
expectMsgClass(SendEmailActor.SendEmailResult.getClass)
but I got test fail.
java.lang.AssertionError: assertion failed:
expected class vndirect.elasticemail.actor.SendEmailActor$SendEmailResult$,
found class vndirect.elasticemail.actor.SendEmailActor$SendEmailResult
So my SendEmailActor return a SendEmailResult as expected. But the test unit fail. I don't know why. What is difference between SendEmailActor$SendEmailResult$ and SendEmailActor$SendEmailResult
You probably have something like
object SendEmailActor {
...
case class SendEmailResult(some arguments)
object SendEmailResult // may be compiler generated
}
class SendEmailActor {
// has ... ! SendEmailResult(...) in some method
}
SendEmailActor.SendEmailResult.getClass is the class of object SendEmailResult. The class of class SendEmailResult is classOf[SendEmailActor.SendEmailResult], so that's the one you should expect.
I came to this question with a similar problem but the suggested answer didn't work. My task was to check for the arrival of a simple case class used as an Akka message.
The solution was to use:
expectMsgType[Message]
instead of expectMsgClass. (Note "Type" instead of "Class".)

Play 2.3 - Changing to WebSocket.tryAccept from async

I'm new rather new to Scala so I think this might be a very small problem.
I'm currently trying to change the method chat from using the deprecated WebSocket.async to WebSocket.tryAccept. The application uses the sample chat found at PlayFramework websocket-chat
I'm having trouble creating the complex Future type that the method requires.
This is the old method:
def chat() = WebSocket.async[JsValue] {
request =>
ChatRoom.join("User: 1")
}
New method:
def chat2() = WebSocket.tryAccept[JsValue] {
request =>
try {
// ChatRoom.join returns (iteratee,enumerator)
ChatRoom.join("User: 1").map(e => Right(e))
} catch {
case e: Exception =>
Left(Ok("Failed")) // Error here
}
}
My error message:
found : Left[Result,Nothing]
required: Future[Either[Result,(Iteratee[JsValue, _], Enumerator[JsValue])]]
I have no idea how I am supposed to create such a complex result for such a simple message.
Although ChatRoom.join("User: 1").map(e => Right(e)) doesn't show any errors now, I'm unsure if this is the correct implementation.
I'm not in front of an IDE at the moment, so I can't answer fully, but the return type it's asking for isn't as complex as it seems. An "Either" is a "Left" or a "Right" in the same way that an "Option" is a "Some" or a "None". So what it's asking for is a Future (which Websocket.async should also have required) that contains either a Left[Result] -- the fail-to-connect case, or a Right[(Iteratee, Enumerator)] -- the success case. Assuming that Chatroom.join returns a Future[(Iteratee, Enumerator)], the map operation is simply wrapping that in a "Right". The first thing I'd try is wrapping Left(Ok("Failed")) in a Future and see what happens.

Compiler error while converting java Exception code to scala using intercept : ''forward reference extends over definition of value exception'

How to convert exception catching code from java to scala using intercept ?
From reading http://www.scalatest.org/getting_started_with_fun_suite it seems to be recommended to use intercept when catching exceptions. In the exception catching code below I need to access the Exception type so as to check its type within the
assert :
catch {
case me : MyException => {
assert(me.getDetail.getClass() === classOf[GenericException]);
}
}
This does not seem to be possible using intercept as below code causes a compiler error : 'forward reference extends over definition of value exception'
Here is the converted to scala catch block :
val exception = intercept[MyException] {
assert(exception.getDetail.getClass() === classOf[GenericException]);
}
The error occurs on line
assert(exception.getDetail.getClass() === classOf[GenericException]);
Well your definition of exception is circular. What you want is:
val exception = intercept[MyException] {
<your exception-throwing code here>
}
assert(exception.getDetail.getClass() === classOf[GenericException])

Early return from a Scala constructor

I am writing the constructor for my "main" class. The first thing it does is call a method to use commons-cli to parse the command line. If the parseOptions method returns false, an error has occurred, and the constructor should exit.
I tried writing the following code
if (!parseOptions(args)) return
but the compiler complains that I have a "Return statement outside method definition".
Short of calling System.exit(1) or inverting the boolean (and putting all of the rest of my logic inside the if statement, is there any way to return "early" from a constructor?
I suppose I could have the parseOptions method throw an IllegalArgumentException and catch that in my Main object.
Thanks.
Dont try to do a early/premature return, this makes your code harder more complex, since the side effects of the return can be hard to understand. Instead use a exception to signal that something is wrong.
You can use require in the constructor. This doesn't return. But it seems like throwing an exception actually fits his situation better.
As in:
class MyTest(
private var myValue: Int ){
require(myValue > 0) // Connected to constructor
}
defined class MyTest
scala> val x = new MyTest(10)
x: MyTest = MyTest#49ff4282
scala> val y = new MyTest(-10)
java.lang.IllegalArgumentException: requirement failed
at scala.Predef$.require(Predef.scala:133)
is there any way to return "early" from a constructor
No. But in your case it sounds like bad design, anyway.
If the parseOptions method returns false, an error has occurred
In this case the constructor should throw an exception, not return normally.
A constructor should always either complete fully, or abort (throw an exception). Anything else leaves your object "half constructed" and thus impossible to reason about.
If in your case, the object is valid even if parseOptions failed, then you can change the condition and continue:
if (parseOptions(args)) {
// rest of constructor
}