Im working on a Spring Boot java service that contains a Camel Processor class as follows:
public class MyProc implements Processor {
@Autowired
private LogService logService;
public void process(Exchange e) {
// exchange object processing
logService.update(e)
}
}
And I have the following Spock test:
class MyProcTest extends Specification {
@Shared def logService = Mock(LogService)
@Shared def proc = new MyProc()
def ctx = new DefaultCamelContext()
def exch = new DefaultExchange(ctx)
void setupSpec() {
proc.logService = logService
}
def "log is updated when valid exchange is processed"() {
given:
exch.getIn().setBody(//xml string set on body)
when:
proc.process(exch)
then:
1 * logService.update(_)
}
}
When I run this, I get a failure, stating too few invocations for 1 * logService.update(_) (0 invocations). I tried debugging the code, and in MyProc, the statement is hit, and the logService object when highlighted(in Eclipse) states 'Mock for type LogService named $spock_sharedField_logService', so it looks like the mock has been successfully injected into the MyProc instance.
Im new to Spock and to Groovy so I may be missing something, but shouldn't thetest pass? The mocks method is being invoked when the test is ran, so I don't understand why the test is reporting back that the mocks method has not been called at all. Am I initialising the mock/setting the mock on the MyProc instance/ setting the interactions incorrectly? Is there some Groovy feature or caveat that I've missed? From what I understand, Spock mocks will return a default value from a method when called, which is fine, I just want to check that this particular method has been called as part of proc.process with a valid exchange object
@Shared? It looks like you're instantiating the mock once (you usually reset it per-method) but then reconstructing the context each time. This mismatch could be causing the problem. - chrylis -cautiouslyoptimistic-@Sharedexplicitly means "don't make a new copy for each feature method". - chrylis -cautiouslyoptimistic-