Stefan Kendall
Stefan Kendall

Reputation: 67802

Passing a service to non-service in grails?

I have a non-service class which is defined as such:

class A{
   B b
   A( B b ){ this.b = b }
}

where B is a grails service. In my unit tests, I tried this:

A a = new A( new B() );

For some reason, however, b never gets set, and the variable b [local, the argument to the mehod] isn't even visible in Intelli-J's debugger when running the test. That is, I can rename the argument to service, and the debugger shows it as undefined.

When I try to start a server, I get Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException, so I'm assuming this is related.

What's going on here?

Upvotes: 0

Views: 250

Answers (1)

Paul
Paul

Reputation: 142

That seems like a strange scenario. What is A doing? Is there any chance that A is really a service?

I'd suggest making A a service, in which case you can inject the B service into it as per normal usage.

I don't think that doing 'new B()' would really be valid, unless B has no dependencies on anything (autowired fields). Perhaps injecting B into the test would be better?

Upvotes: 2

Related Questions