20 October 2008

JUnit Testing Scala in Eclipse

Being a fairly new convert to agile development methods, including test driven development, I knew the first thing I would want to get up and running with Scala was a unit testing framework. The NetBeans plugin has built-in JUnit support which served me very well. But I also wanted to get some unit testing working with the Eclipse plugin. It took me a while to get JUnit testing setup with Eclipse, mainly due to my lack of experience in the Java ecosystem. To help future explorers down this path, here are a few lessons I learned.

Put the your tests in a separate source folder. See this tutorial on setting up JUnit in Eclipse for more information.

Add JUnit to your project's build path. Select the project and choose "Build Path" -> "Add Libraries...". Then choose the JUnit jar.

Add the output directory of your project to your build path. See this short guide from the makers of Spec.

To get you started, here is a simple test:
package tests

import junit.framework._
import org.junit.Assert._

class FirstTest extends TestCase {
override def setUp() = {
}

override def tearDown() = {
}

def testOne() = {
assertEquals(1, 1)
}
}

2 comments:

mateusz.fiołka said...

I tried to do the same with Junit4. It works fine *however* after I restart Eclipse the runner doesn't find test methods. I need to add the output directory of my project to my build path again. Do you have the same problem? If yes, maybe you have some way to fix it?

Unknown said...

Jau,

I tried to recreate your problem, but that doesn't seem to happen to me. I'd suggest the Scala Nabble forums or Eclipse forums. I'm definitely no Eclipse expert, but many people there are. Good luck, and let me know what you figure out so I can amend my post.

--Kevin