Search code examples
unit-testingscalascala-2.8junit4

How to run scala tests with junit 4?


I can't run following code with IDEA

@Test
class CompanyURLTest extends Assert {
  @Test
  def test = assert(false);

}

It runs, but J-Unit says that there are not test to run


Solution

  • I generally use ScalaTest in combination with the Junit4 runner so that Maven sees and executes my tests. I like the Spec/FlatSpec/WordSpec semantics for organizing tests. I'm experimenting with the ShouldMatchers but I have used JUnit for so long that asserts just seem a bit more natural to me.

    Here's an example:

    import org.junit.runner.RunWith
    import org.scalatest.junit.JUnitRunner
    import org.scalatest.FlatSpec
    import org.scalatest.matchers.ShouldMatchers
    
    @RunWith(classOf[JUnitRunner])
    class BlogFeedHandlerTest extends FlatSpec with ShouldMatchers with Logging {
        "the thingy" should "do what I expect it to do" in {
            val someValue = false;
    
            assert(someValue === false)
        }
    }
    

    The ScalaTest docs are at http://www.scalatest.org/