Reputation:
I am getting started with Groovy and I don't know how to write unit tests over my methods written on a class. For example:
One my methods on sample.groovy :
class sample {
def length = { list -> list.size() }
...
}
So, I have one class named sampleTest.groovy :
class sampleTest extends GroovyTestCase {
@Test
public void testLength() {
def result = length([2, 3, 8, 9, 0, 1, 5, 7])
assert result == 8
}
}
BTW, when I run this test, an error throws to me:
groovy.lang.MissingMethodException: No signature of method: sampleTest.length() is applicable for argument types: (java.util.ArrayList) values: [[2, 3, 8, 9, 0, 1, 5, 7]]
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
...
Upvotes: 2
Views: 20112
Reputation: 187399
length
is a (non-static) property of the sample
class. Because it's non-static, you need to evaluate it against a sample
instance, e.g.
class sampleTest extends GroovyTestCase {
@Test
public void testLength() {
def result = new sample().length([2, 3, 8, 9, 0, 1, 5, 7])
assertEquals 8, result
}
}
Also, you should try to use the assertion methods provided by GroovyTestCase
such as assertEquals
, assertNotNull
, assertTrue
, rather than the assert
keyword.
You should also capitalize your class names, e.g. Sample
and SampleTest
rather than sample
and sampleTest
.
Upvotes: 4