#Test method in Scala trait not found - scala

does somebody know why #Test annotated methods which are inherited from a Scala trait are not found by the JUnit 4 test runner? It gives me "No JUnit tests found".
class FooTests extends BarTesting
But the following modification works?
import junit.framework.TestCase
class FooTests extends TestCase with BarTesting
In the Scala trait BarTesting I defined methods with the following signature.
#Test
final def testBar() {
// ...
}

This is indeed a bug in Eclipse. You can raise as such if you like. http://www.assembla.com/spaces/scala-ide/tickets.
When you extend TestCase the test is being run because it starts with test, not because of the annotation. There was a problem with recognition of annotations, which is how the junit stuff works, and I haven't looked to see if it is fixed yet to make the junit stuff work.
Your best bet is to:
Raise the bug against scala-ide
Add #RunWith[classOf[JUnit]) to your class
The following works:
trait BarTesting {
#Test final def testBar() {
println("Hello world")
}
}
#RunWith(classOf[JUnit4])
class FooTesting extends BarTesting {
}
And I'll try and fix the bug.
EDIT: In the latest versions of scala-ide (as of 9 November 2011), this now works.

Related

Scalatest with eclipse shows errors while using Matchers

I have an eclipse scala project which uses maven. Eclipse plugins for ScalaIDE and Scalatest are installed. I have tests like:
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
feature("Feature A Test") {
scenario("Foo scenario 1") {
val a = FooClass().getResult()
a.count shouldBe 1 // IDE shows error: value shouldBe is not a member of Long
a(0).getString(0) shouldBe "FOO" // IDE shows error: value shouldBe is not a member of String
}
}
}
The maven compilation and the tests run fine, but in eclipse when I open this file, I see an error in eclipse wherever I am using a Matcher as mentioned in the comments above. Eg.
value shouldBe is not a member of Long
What am I missing? A scala test file shows hundreds of problems.
After adding the following dummy code:
case class Bar() {
def count = Array(Bar())
def getString(x: Int) = Array("aqq")
def apply[T](x: Int) = this
}
case class FooClass() {
def getResult() = Bar()
}
and changing FlatSpec to FeatureSpec as this is the syntax you are using in your ExampleSpec, the code compiles without any issues.
If it's still not the case for you I can suggest creating simple build.sbt and generating project with Eclipse sbt plugin.
I know this is old, but I had the same issue with eclipse (late 2018), and I was able to fix this by making sure the test was NOT in the default package. That is, add "package org.scalatest.examples.flatspec" to the beginning of your test, as an example, and move the test into that package.

PowerMockito throwing ClassNotPreparedException even with #PrepareForTest using Scala Test

I have the following test...
import org.scalatest.junit.JUnitRunner
...
#PowerMockRunnerDelegate(classOf[JUnitRunner])
#PrepareForTest(Array(classOf[AuditLog]))
class ConnectorAPITest extends path.FreeSpec with ShouldMatchers {
"Mocked Tests" - {
println("This got called in the mocked tests.")
PowerMockito.mockStatic(classOf[AuditLog]);
...
}
}
But when I run I get...
An exception or error caused a run to abort: The class com.paxata.services.log.AuditLog not prepared for test.
To prepare this class, add class to the '#PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or method level.
org.powermock.api.mockito.ClassNotPreparedException:
The class com.paxata.services.log.AuditLog not prepared for test.
To prepare this class, add class to the '#PrepareForTest' annotation.
Which doesn't make sense given the annotation is already there? Is it an idiosyncrasy of Scala test?
I had the same problem using FunSuite. It works when I turned to JUnit.
#RunWith(classOf[PowerMockRunner])
#PrepareForTest(Array(classOf[SomeStaticClass]))
class MyTestClass {
#Before
def setUp {
PowerMockito.mockStatic(classOf[SomeStaticClass])
Mockito.when(SomeStaticClass.getSomeObject(1)).thenReturn(new SomeObject(1))
}
#Test
def someTestMethod {
}
etc...

How to make JUnit4 lifecycle callbacks work in ScalaTest with JUnitRunner?

According to ScalaTest documentation it's possible to use JUnitRunner to run tests. My assumption was, if it runs with JUnitRunner, its callback methods (i.e. methods marked with #Before or #After annotation should work as well. But apparently my assumption was wrong. I've created simple example to demonstrate it:
import org.junit.Before
import org.junit.runner.RunWith
import org.scalatest.{FunSuite, _}
import org.scalatest.junit.JUnitRunner
#RunWith(classOf[JUnitRunner])
class Test extends FunSuite {
#Before
def before() = {
println("before test")
}
test("nothing") {
println("test is started")
}
}
If you run this example, you'll see test is started line, but not before test.
I'm aware of ScalaTest lifecycle callbacks, but the thing is I need to make JUnit callbacks work somehow. I want to write a test for Play 2.4 application and the thing it's play.test.WithBrowser class relies on JUnit callbacks. I found this workaround for the issue:
var testBrowser : TestBrowser = _
before {
new WithBrowser {
override def createBrowser(): Unit = {
super.createBrowser()
testBrowser = browser
}
}.createBrowser()
}
but I believe it's quite ugly and I suspect that there is a better way.
So, my question is if it's possible to make these JUnit lifecycle callbacks work with ScalaTest? And if it is possible, how to do that?
You should be able to do this using JUnitSuite instead of FunSuite. The documentation is here:
http://www.scalatest.org/getting_started_with_junit_4_in_scala
http://doc.scalatest.org/2.2.4/index.html#org.scalatest.junit.JUnitSuite
I did just like Noah suggested and it worked. Just in case anyone stumbles upon the same issue with Play 2.x and WithBrowser, I ended up with a test like this:
class IntegrationSpec extends WithBrowser with JUnitSuiteLike with Matchers {
#Test def workWithBrowser() {
browser.goTo("http://localhost:9000")
browser.pageSource should include("Add Person")
}
}

Mockito ignores my Specs2 sugared verify steps when traits are involved

Normally Specs2 sugared Mockito verifications are checked and fails the test when appropriate. However in some instances they are ignored.
Normally this test verification fails as expected as myApp called myService at least once.
import org.specs2.mock.Mockito._
class MySpec extends Specification with Mockito {
"MyApp" should {
"blow up" in WithApplication {
val myService = mock[MyService]
val myApp = new MyApp(myService)
myApp.doSomething
there was no(myService).doSomethingElse
}
}
}
(Note WithApplication is a Play! Framework thing)
However as I have hamfisted Cake Pattern traits into my components my tests look like this.
class MySpec extends Specification with Mockito {
"MyApp" should {
"blow up" in WithApplication with MockRegistry {
val myApp = new MyApp(myService)
myApp.doSomething
there was no(myService).doSomethingElse
}
}
}
where MockRegistry looks something like this
trait MockRegistry extends Mockito with MyServiceComponent {
val myService = mock[MyService]
}
My Cake patterned test does not fail verification, ever. I can change this to anything and they all get ignored.
there was no(myService).doSomethingElse
there was one(myService).doSomethingElse
there was two(myService).doSomethingElse
However by replacing the sugared mockito step with a direct call to the java methods it does fail when appropriate.
import org.mockito.Mockito._
verify(myService, times(1)).doSomethingElse
So it seems involving traits on the test method seems to have properly confused Mockito.
That's because the Mockito extension of MockRegistry doesn't know that exceptions needs to be thrown in case of failure. But the Mockito on Specification does because Specification has the ThrownExpectations trait mixed-in.
So you can either removed the Mockito extension from MockRegistry or add ThrownExpectations to it:
trait MockRegistry extends MyServiceComponent {
val myService = mock(classOf[MyService])
}
// or
import org.specs2.matcher.ThrownExpectations
trait MockRegistry extends MyServiceComponent with ThrownExpectations {
val myService = mock(classOf[MyService])
}
And as expected it was due to the traits. It was in face due too many Mockito trait-ing...
My spec had a mockito trait. My cake pattern mocked component registry also had a mockito trait. On the test method they were both part of the object, which seems to confuse Specs2/Mockito.
So by removing Mockito sugar from my mock component registry:
//import org.specs2.mock.Mockito._
import org.mockito.Mockito._
trait MockRegistry extends MyServiceComponent {
val myService = mock(classOf[MyService])
}
And only have the Mockito sugar in the Spec then my sugared verifications started to work as expected again.

Final clean up in specs2

I am writing a specs2 Unittest for my scala software. The execution is working well. The only problem I have, is that I need to clean up after all test are finished. I just cannot find any solution for that. Is there a way to execute some functions after all test are finished?
You need to add a Step at the end of your specification:
import org.specs2.mutable._
class MySpec extends Specification {
// lots of examples here
// cleanup there
step(cleanUp())
}
You can try to use After with After and implement def after function.
Example:
class Context extends Specification {
....
}
trait trees extends mutable.After {
def after = cleanupDB
}