I’m using Groovy 1.8.6 and Grails 2.1.1
I have a interface
public interface Searchable{
Long docVersion()
}
Implemented by a object
class Book implements Searchable {
Long docVersion() {
System.currentTimeMillis() / 1000L
}
String otherMethod() {
"toto"
}
}
And a test
@Mock([Book])
class SomeBookTester {
@Before
void setup() {
Book.metaclass.docVersion = {-> 12345}
Book.metaclass.otherMethod = {-> "xyz"}
}
@Test
void test1() {
assert 12345 == new Book().docVersion()
}
@Test
void test2() {
assert "xyz" == new Book().otherMethod()
}
}
The first test always fail because the methode replacement dosen’t work. How could I fix this? What’s the probleme?
This works for me
I change the class like that:
And in the test, I replace the currentTime method
Tests passes