score:1
one possible solution would be to use intercept
method:
val exception = intercept[nosuchelementexception] {
list.empty[string].head // code that throws exception
}
exception.getmessage shouldbe "head of empty list"
score:1
you can you the "an [] should be thrownby" matcher:
an [illegalargumentexception] should be thrownby {
//code that should raise an exception here
}
make sure your test class includes the "matchers":
class mytestclass extends funsuite with matchers
Source: stackoverflow.com
Related Query
- How to match type of instance in ScalaTest
- How to instantiate an instance of type represented by type parameter in Scala
- How to pattern match on generic type in Scala?
- How do I get an instance of the type class associated with a context bound?
- How to create an instance of type T at runtime with TypeTags
- How to solve type mismatch when compiler finds Serializable instead of the match type?
- How to create an instance of a generic type in a Scala 3 macro?
- How can I write a Scalatest test to match the values in a list against a range?
- scala pattern match a function - how to get around type erasure
- How do I constrain a member of a parameter class to match the instance of an outer class?
- How do I write a match type pattern with a narrowed String type head of tuple case in dotty?
- How can I match a function signature without getting type erasure compiler warnings in Scala
- How to find instance of a value type in Scala?
- How to see the type of a scala variable? For instance a Spark PairRDD
- ScalaTest - how to check that all items of sequence match the predicate?
- How can I parameterize a class with an object's type to get an instance of it in Scala?
- Summoning instance for opaque type in an object with match type fails
- How to get match type with multiple type parameters to work correctly in Scala 3
- How to match type parameter of a function in Scala
- How do you check if a runtime instance conforms to some type using the Scala 2.10 reflection API?
- How to match on unknown generic type without warnings
- How to specify the type introduced in a match of class with independent type parameter
- How to ensure two parameters in an inline type match (scala 3) refer to the same type
- How to compare result type to be subclass of abstract class to match to?
- How can I create an instance of projected type in Scala?
- How to get the matched instance of case class inside match pattern
- How to match the type of an array in Scala?
- How to preserve original type in match without resorting to asInstanceOf?
- How to make a parent class return a type which can match subtypes?
- How to get match type to work correctly in Scala 3
More Query from same tag
- How to split routes in Akka Http
- what is the "Value" when define a enum
- Explicit Type Conversion in Scala
- Outlier Elimination in Spark With InterQuartileRange Results in Error
- Making a Hibernate @ManyToOne association a Scala Option type
- How to deploy scala files used in spark-shell on cluster?
- Multiple late initialisation
- Where can I find the documentation about using type parameters as function parameters?
- How to make merge or intercalate two maps in scala one by one?
- SPARK_EXECUTOR_INSTANCES not working in SPARK SHELL, YARN CLIENT MODE
- spark standalone application out of memory on small text files
- Akka complains to a generic parameter
- scalatest : object scalatest is not a member of package org
- "is not a member of package" error when importing package in Scala with SBT
- Scala: How do I set a generic Trait?
- Scala generic implicit val
- Spark dataframe Join issue
- Convert string to timestamp for Spark using Scala
- spark-shell "error: type mismatch" using :paste to define class/companion object
- Scala delegate import of implicit conversions
- Problems using Scala set collection from Java
- Adding contents in an RDD[(Array[String], Long)] into a new array into a new RDD: RDD[Array[(Array[String], Long)]]
- How to submit Spark jobs to Apache Livy?
- Trait method with free parameters
- Pattern match empty ArrayBuffer
- Discrete state and animated transitions
- Stream processing
- In scala, how I can decide between class + companion object vs just the object
- Error while reading very large files with spark csv package
- Cannot instansiate BinaryClassificationMetrics class in Spark