2011-01-02 6 views
6

Chciałbym opisać testy w stylu BDD, np. z FlatSpec, ale zachowaj JUnit jako biegacz testowy.Czy można używać składni ScalaTest BDD w środowisku JUnit?

ScalaTest Skrócona wydaje się nie wykazują żadnych przykład tak:

http://www.scalatest.org/getting_started_with_junit_4

raz pierwszy spróbował naiwnie do pisania testów w ciągu @Test metod, ale to nie działa, a twierdzenie nie jest testowany :

@Test def foobarBDDStyle { 
    "The first name control" must "be valid" in { 
     assert(isValid("name·1")) 
    } 
    // etc. 
} 

Czy jest jakiś sposób, aby to osiągnąć? Byłoby jeszcze lepiej, gdyby zwykłe testy można było mieszać i dopasowywać do testów BDD.

Odpowiedz

11

Sposób, prawdopodobnie chcesz, aby to zrobić jest użycie adnotacji @RunWith, tak:

import org.junit.runner.RunWith 
import org.scalatest.junit.JUnitRunner 
import org.scalatest.FlatSpec 

@RunWith(classOf[JUnitRunner]) 
class MySuite extends FlatSpec { 
    "The first name control" must "be valid" in { 
     assert(isValid("name·1")) 
    } 
} 

JUnit 4 użyje JUnitRunner ScalaTest do uruchom FlatSpec jako pakiet testowy JUnit.

+0

Czy możliwe jest domyślne uruchamianie wszystkich ScalaTests z JUnitRunner? Powód: chcielibyśmy uruchomić testy z Gradle i najwyraźniej JUnitRunner zapewnia lepsze możliwości integracji. –

6

Nie musisz mieć adnotacji def s i @Test. Oto przykład:

import org.scalatest.junit.JUnitRunner 
import org.junit.runner.RunWith 
import org.scalatest.FlatSpec 
import org.scalatest.junit.ShouldMatchersForJUnit 

@RunWith(classOf[JUnitRunner]) 
class SpelHelperSpec extends FlatSpec with ShouldMatchersForJUnit { 

    "SpelHelper" should "register and evaluate functions " in { 
    new SpelHelper() 
     .registerFunctionsFromClass(classOf[Functions]) 
     .evalExpression(
     "#test('check')", new {}, classOf[String]) should equal ("check") 
    } 

    it should "not register non public methods " in { 
    val spelHelper = new SpelHelper() 
     .registerFunctionsFromClass(classOf[Functions]) 
    evaluating { spelHelper.evalExpression("#testNonPublic('check')", 
     new {}, classOf[String]) } should produce [SpelEvaluationException] 
    } 
} 

Source

Powiązane problemy