seratch / scalatest

Automatically exported from code.google.com/p/scalatest
Apache License 2.0
0 stars 0 forks source link

Strange assertion behavior #18

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Forgive me if I am missing something obvious. I am developing my first full 
fledged Scala project after being a Java developer for 8 years.

I recently upgraded from Scala 2.9.2 to 2.10.0 and upgraded scalatest to:

<dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_2.10.0</artifactId>
            <version>2.0.M5</version>
            <scope>test</scope>
</dependency>

If I write the following test:

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FreeSpec

@RunWith(classOf[JUnitRunner])
class SomethingTest extends FreeSpec {

  "Something" - {

    "something" in {

      val something = null
      assert("something"==something,something)
    }
  }
}

, I would expect to see an assertion failure where "something" is not equal to 
null and a message printed to that effect, but instead I get:

java.lang.NullPointerException was thrown.
java.lang.NullPointerException
    at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:393)

, with an error in the stack trace reference the line containing the 
"assert("something"==something,something)"

I'll admit that I am being lazy in going back to 2.9.2 and the respective 
scalatest jar, but I am pretty sure that the assertion failed as expected 
before I upgraded. I could be completely wrong about this. 

What am I missing here? Is this normal behavior?

Original issue reported on code.google.com by zackdjon...@gmail.com on 6 Jan 2013 at 7:11

GoogleCodeExporter commented 8 years ago
This may be because of a change in Scala from 2.9 to 2.10, I'm not sure, but 
regardless what I'd recommend you write instead is:

assert("something" === something) 

And it will give you a good error message if it fails. I checked the code and 
it will throw an NPE if you pass null as the clue object. This is the specified 
behavior in the Scaladoc, but possibly it would be better if it just 
transformed a null into the message "null". I'll think about that.

Original comment by b...@artima.com on 6 Jan 2013 at 9:59

GoogleCodeExporter commented 8 years ago
Thanks. Again, I am new to both Scala and Scalatest., my expectations for how 
something should work come from the Java world and JUnit. That doesn't make it 
right. I've changed the behavior of some of my assertions with your suggestion 
and using the 'should' matchers.

Original comment by zackdjon...@gmail.com on 6 Jan 2013 at 4:12

GoogleCodeExporter commented 8 years ago
No problem. I would like to to work like you expect. I'll take a look at 
JUnit's behavior when a null "clue string" is passed. 

Original comment by b...@artima.com on 6 Jan 2013 at 10:40