Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an expected exception using Scala and JUnit 4

I want to set an expected exception for a JUnit 4 test using Scala. I am current doing something similar to the following:

@Test(expected=classOf[NullPointerException])
def someTest() = {
    // Some test code
}

But I get the following compiler error:

error: wrong number of arguments for constructor Test: ()org.junit.Test
like image 322
Michael Barker Avatar asked Jun 27 '09 07:06

Michael Barker


2 Answers

This is looking forward a bit, but the syntax for annotations in 2.8 has changed to be the same as what you originally posted. The syntax Tristan posted is correct in the current stable version, but it gave me errors when I upgraded my project to a nightly 2.8 compiler. I'm guessing this is due to the inclusion of named and default arguments. There is also some discussion on the Scala mailing list. Quoting Lukas Rytz:

Also note that in 2.8.0 the syntax for java annotations will no longer use the name-value pairs but named arguments instead, i.e.

@ann{ val x = 1, val y = 2}  ==>  @ann(x = 1, y = 2)
like image 110
Jay Conrod Avatar answered Oct 25 '22 17:10

Jay Conrod


The way scala deals with attributes is a little funky. I think what you're trying to do should be expressed like this:

@Test { val expected = classOf[ NullPointerException] }
def someTest {
    // test code
}
  • Scala language page with many annotation examples.
like image 24
Tristan Juricek Avatar answered Oct 25 '22 17:10

Tristan Juricek