Scala Confusion

Just for something to do, I’m learning? Scala. Having deep confusion making JUnit suites run. Advise me in comments if you’re up for it?

Reading Venkat’s book, and Dean Wampler’s book, I decided that rather than run things at the command prompt … how very last century, I’d write a bunch of JUnit tests to test and document the things I was trying. It seems to me to be a much better way to do things.

(Why am I not using ScalaTest or another such system? Largely because I know JUnit and felt no desire to try to learn two things at once. And yes, I know I should get a Mac.)

It’s going pretty well so far, or was until I tried to set up a suite of tests. I’ve got a big JUnit test file, and it was just getting irritating to add another test to it, so I started another one, thinking maybe someday they’d even get organized. But of course I’d like to run all the tests frequently just because I like the jolt of the green bar.

So I looked up how to do a Suite. There’s nothing really solid on the Web but I found a couple of consistent items. They are almost working but some of it has me confused. I’ll note down here what I’ve got and maybe if you grok Scala you can advise me in the comments.

I have my main test file. It looks like this:


import org.junit._
import org.junit.runner._
import org.junit.runners._
import Assert._
import StringUtil._

class TestSomething { 

	@Test def hookup() {
		assertEquals(2,2)
	}

	@Test def someArrayStuff {
		val array: Array[String] = new Array(5)
		array(0) = "abc"
		assertEquals("abc", array(0))
		assertEquals(null, array(1))
	}
        ...
}

There are a couple of class files, not relevant here. There’s another test file that I just started:


import org.junit._
import org.junit.runner._
import org.junit.runners._
import Assert._ 

class TestMore {
	@Test def hookup4 {
		assertEquals(4,4)
	}
}

As soon as I started with that, I realized I wanted a suite, to run everything. I have that in place, like this:


import org.junit._
import org.junit.runner._
import org.junit.runners._
import Assert._ 

@RunWith(classOf[Suite])
@Suite.SuiteClasses(Array(classOf[TestDummy]))
class TestEverything

class TestDummy {
	@Test def hookup {

	}
}

This is lifted from a couple of examples I found on the Web. I’ve fiddled a bit with what is in the RunWith class, and what is in the SuiteClasses array. None of them seems to work unless I have at least a dummy test in the same file, and I seem to require the empty class (TestEverything) as well.

As this stands, all the tests run and show green or red as they should. JUnit’s Eclipse GUI looks like this:

Notice that TestDummy shows up twice, once in the top-level listing where the other tests are. But at that level TestDummy does not have green check marks. It shows up again, under TestEverything, and there it will have checks as appropriate. I’ve fiddled with not having any tests in the TestEverything file, and that seems to fail entirely. I’ve tried various names, or putting the names of the other test classes in the @Suite entry. In every case, I either get errors or an odd display where some of the classes show up twice, on their own or under TestEverything.

Advise me, please … have you a configuration of JUnit with Scala with Suites? I’d love to see how it works.

And OK, let me know what testing tool you’re using with Scala as well. But I’m not going to get a Mac.

Thanks!

Posted on:

Written by: Ron Jeffries

Categorization: Articles

8 Responses to “Scala Confusion”

Ron Jeffries

July 28, 2010

8:14 am

permalink

Yes … I could plug in ScalaTest and quite possibly I should. Does it have Suites? If, as it looks, the issue is with JUnit, am I likely to have the same issues?

I don’t usually recommend solving a problem by adding more unknowns to the mix … that’s my concern here.

Thanks … it may come down to switching horses. Or metaphors.

Jamie Whitehouse

July 28, 2010

9:20 am

permalink

Instead of creating a suite you can use Eclipse to run all tests in a package. Select the package, run as, junit test (shift-alt-x t). After that ctrl-f11 will re-run it.

Or get Kent Becks JUnit Max that will automatically run your tests when files are saved.

Ron Jeffries

July 28, 2010

9:32 am

permalink

Duh. That works just fine. I think I’ll stop worrying about making the Suite right. Still curious how to do it the other way tho …

Thanks!

Jamie Whitehouse

July 28, 2010

10:07 am

permalink

Creating a suite using JUnit for worked for me:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({SystemSpeakerTest.class, StringSplitSpeedTest.class})
public class AllTests {

}

See the JavaDoc at http://kentbeck.github.com/junit/javadoc/latest/org/junit/runners/Suite.html

Note: if using the Eclipse run package and this suite is in the same package with the tests then they’ll get run twice, once for the suite and once for the individual test class.

Tim Taylor

July 28, 2010

12:03 pm

permalink

The IDE support for run-all-tests is nice, but I’d still like to know how to do a Suite properly, in my case for automated/CI builds.

Ron Jeffries

August 8, 2010

7:29 am

permalink

Closing comments on some of these older articles. Later ones still timely and open …

Recent Articles

Scala Bowling, I think …

I’ve been working with Scala a bit, just to learn what it is. I’ve found it interesting, if frustrating. Here is a bowling experiment.