如何使用 @InjectMocks 注解来注入具有相同类型的依赖项?

huangapple go评论87阅读模式
英文:

How to use @InjectMocks to inject dependency with same type?

问题

以下是您提供的内容的翻译部分:

我有一个名为 User 的类,其构造函数有两个相同类型的参数。

public class User {
    Dependency dependency1;
    Dependency dependency2;
    User(Dependency dependency1, Dependency dependency2){
        this.dependency1 = dependency1;
        this.dependency2 = dependency2;
    }
    public void test(){
        dependency1.print();
        dependency2.print();
    }
}

在我的测试中,我有两个 Spy 依赖项,我希望它们像 new User(dependency1,dependency2) 这样被注入。

@ExtendWith(MockitoExtension.class)
public class InjectMocksTest {
    @InjectMocks
    User user;
    @Spy
    Dependency dependency1 = new Dependency("dependent1");
    @Spy
    Dependency dependency2 = new Dependency("dependent2");
    @Test
    void test(){
        user.test();
    }
}

但我发现 User 的 dependency1 和 dependency2 都指向测试中的 dependency1,就好像它们是通过 new User(dependency1,dependency1) 进行注入的。

那么,如何使用 @InjectMocks 注解来实现我想要的效果呢?

英文:

I have a User class which has a constructor with two parameter of same type.

public class User {
    Dependency dependency1;
    Dependency dependency2;
    User(Dependency dependency1,Dependency dependency2){
        this.dependency1=dependency1;
        this.dependency2=dependency2;
    }
    public void test(){
        dependency1.print();
        dependency2.print();
    }
}

In my test, I have two Spy Dependency and I want them to be injected like new User(dependency1,dependency2).

@ExtendWith(MockitoExtension.class)
public class InjectMocksTest {
    @InjectMocks
    User user;
    @Spy
    Dependency dependency1=new Dependency("dependent1");
    @Spy
    Dependency dependency2=new Dependency("dependent2");
    @Test
    void test(){
        user.test();
    }
}

But I find that dependency1 and dependency2 of User both refers to dependency1 in the test, like they are injected with new User(dependency1,dependency1).

So how to achieve what I want with @InjectMocks annotation?

答案1

得分: 1

它似乎可以通过使用字段注入来欺骗Mockito将模拟对象注入,如果您首先创建对象本身,并将参数设置为null。这对我有效:

@ExtendWith(MockitoExtension.class)
public class UserTest {
  @InjectMocks
  User user = new User(null, null);

  @Spy
  Dependency dependency1 = new Dependency("dependent1");
  @Spy
  Dependency dependency2 = new Dependency("dependent2");

  @Test
  void test() {
    user.test();
  }
}

输出:
dependent1
dependent2

然而,这种行为没有记录,所以我不确定是否应该依赖它。
英文:

It appears that you can trick Mockito into injecting the mocks using Field injection if you create the object itself first, and set the arguments to null. This works for me:

@ExtendWith(MockitoExtension.class)
public class UserTest {
  @InjectMocks
  User user = new User(null, null);

  @Spy
  Dependency dependency1 = new Dependency("dependent1");
  @Spy
  Dependency dependency2 = new Dependency("dependent2");

  @Test
  void test() {
    user.test();
  }
}

Output:

dependent1
dependent2

However, this behavior is not documented, so I am not sure I would depend upon it.

huangapple
  • 本文由 发表于 2020年10月20日 14:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64439853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定