Using Specs with unitils
I am trying to get scala specs and unitils-dbunit to work.
To use unitils you have to annotate your test class with @RunWith(classOf[UnitilsJUnit4TestClassRunner]) or extend from a class and you can set DataSet's to load with @DataSet(Array("DataSet.xml")). All of this works with JUnit.
But I got 2 problems when using specs:
- I am running my specs tests with gradle and the ScalaTestAntTask, so I guess the @RunWith annotation will be ignored.
I can not set the @DataSet annotation to my test method as seen here:
[...] @RunWith(classOf[UnitilsJUnit4TestClassRunner]) class DaoTest extends Specification with ScalaTest { @TestDataSource var dataSource: DataSource = null @DataSet(Array("DataSet.xml")) "querying the database" should { "return the right data" in { [assertSomething] } } }
This gives me the following compiler error:
error: expected start of definition
"querying the database" should {
^
Does somebody know how I can use specs and unitils together?
UPDATE: This is the solution I came up with:
import org.specs.runner.ScalaTest
import org.specs.Specification
import org.unitils.dbunit.DbUnitModule
import java.io.File
import java.util.Properties
import org.unitils.core.ConfigurationLoader
class DaoTest extends Specification with ScalaTest {
"querying the database" should {
doBefore {
UnitilsDatabaseUtils.setup("DataSet.xml", "DataSet2.xml")
}
"return the right data" in {
[test something]
}
}
}
object UnitilsDatabaseUtils {
def setup(dataSetFileNames: String*) = {
val configuration: Properties = new ConfigurationLoader().loadConfiguration
val dbunitModule = new DbUnitModule
dbunitModule.init(configuration)
for (dataSetFileName <- dataSetFileNames) {
val dataSetURL = getClass.getClassLoader.getResource(dataSetFileName)
dbunitModule.insertDataSet(new File(dataSetURL.toURI))
开发者_如何学Go }
}
}
-- Christian
The DataSet annotation can't be applied directly to "xxx" should
because this is not a test method, just a regular scala code call.
I can't give you a fully working solution but what you need to do is to dig out the unitils API and use the DataSetFactory directly to load your data. I think that you can more or less reuse the code found here.
Then, if you need to load this dataset before each example, just use a doBefore call.
精彩评论