Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject via constructor does not behave as expected for collaborators of the same type #28

Open
CiprianSt opened this issue Nov 26, 2021 · 0 comments

Comments

@CiprianSt
Copy link

Let's say you have the next piece of Java code:

public class SomeClass {
   private final SomeOtherClass service1;
   private final SomeOtherClass service2;

   public SomeClass(SomeOtherClass service1, SomeOtherClass service2) {
      this.service1 = service1;
      this.service2 = service2;
   }
}

and the next test specification:

class SomeClassSpec extends Specification {
   @Subject
   SomeClass someClass

   @Collaborator
   SomeOtherClass service1;

   @Collaborator
   SomeOtherClass service2;
}

When the spec is run, the library will use the first collaborator (i.e. service1) for both constructor parameters, so in the end, both instances in SomeClass will point to service1. I do not know if this is intended, but that is the result. In my opinion, the library should also take into account the field names when there are multiple collaborators of the same type.

I know there is a workaround, which is

   @Subject
   SomeClass someClass = Spy(constructorArgs: [service1, service2])

but this workaround defies the purpose of this library and it also goes against Spock recommendations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant