How to use JUnit ExpectedException in Scala?

I would like to use JUnit 4.7 ExpectedException @Rule in Scala. However, it seems he didnโ€™t catch anything:

import org.junit._ class ExceptionsHappen { @Rule def thrown = rules.ExpectedException.none @Test def badInt: Unit = { thrown.expect(classOf[NumberFormatException]) Integer.parseInt("one") } } 

This still does not work with a NumberFormatException .

+6
scala exception junit
source share
5 answers

EDIT: after the release of JUnit 4.11, you can now annotate the method with @Rule .

You will use it as:

 private TemporaryFolder folder = new TemporaryFolder(); @Rule public TemporaryFolder getFolder() { return folder; } 

For earlier versions of JUnit, see the answer below.

-

No, you cannot use this directly from Scala. The field must be open and non-static. From org.junit.Rule :

public @interface Rule: Annotates fields that contain rules. Such a field must be public, not static, and a subtype of TestRule.

You cannot declare public fields in Scala. All fields are private and accessory accessible. See the answer to this question .

In addition, there is already an extension request for junit (still open):

Extend the rules to support @Rule public MethodRule someRule () {return new SomeRule (); }

Another option is to allow non-public fields, but this has already been rejected: Allow @Rule annotation in non-public fields .

So your options are:

  • clone junit and implement the first sentence, method and send a transfer request
  • Extend the Scala class from the java class that implements @Rule

-

 public class ExpectedExceptionTest { @Rule public ExpectedException thrown = ExpectedException.none(); } 

and then inheriting from this:

 class ExceptionsHappen extends ExpectedExceptionTest { @Test def badInt: Unit = { thrown.expect(classOf[NumberFormatException]) Integer.parseInt("one") } } 

which works correctly.

+7
source share

To make this work with JUnit 4.11 in Scala, you must annotate the annotation annotation so that the annotation applies only to the (synthetic) getter method, and not to the base field:

 import org.junit._ import scala.annotation.meta.getter class ExceptionsHappen { @(Rule @getter) def thrown = rules.ExpectedException.none @Test def badInt: Unit = { thrown.expect(classOf[NumberFormatException]) Integer.parseInt("one") } } 
+6
source share

Not knowing the JUnit rules and not testing them, because I donโ€™t have a suitable setting, I go out on a limb and suggest turning the cast into val. I think its member, which is initialized by something, and then gets some state, and then some other machine checks the state against something. You always create new ones and keep forgetting about waiting.

0
source share

If Scala has something similar as static import, then catch-exception is an alternative to JUnit 4.7 ExpectedException @Rule.

0
source share

As a very newcomer to Scala, I just use a very simple workaround: I will explicitly catch the exception and crash if your expected exception is not thrown.

The following is an example of a skeleton:

 try { *your code that should throw an exception* fail("Did not generate *the.Exception.you.expect*") } catch { case t: *the.Exception.you.expect* => // do nothing, it expected :) } 
0
source share

All Articles