用<String>@Spy和@InjectMocks模拟多个向量



>我正在编写一个 JUnit 测试,我需要注入 2 个向量的模拟,如下所示:

@AllArgsConstructor
public abstract class AbstractMyObject {  
    @Autowired
    private Vector<String> firstVector;
    @Autowired
    private Vector<String> secondVector;
    public abstract void  absFunc();
    public void doThis() {
        System.out.println(firstVector);
        System.out.println(secondVector);
    }
}
@Component
public class MyObject extends AbstractMyObject {
    public MyObject(final Vector<String> firstVector, final Vector<String> secondVector) {
        super(firstVector, secondVector);
    }
    @Override
    public void absFunc() {
    }
}
public class TestClass {
    private static String[] array1 = {"Hello", "World"};
    private static String[] array2 = {"junit", "mockito"};
    @Spy
    private Vector<String> firstVector = new Vector<>(Arrays.asList(array1));
    @Spy
    private Vector<String> secondVector = new Vector<>(Arrays.asList(array2));
    @InjectMocks
    private MyObject myObject;
    private void initMocks() {
        MockitoAnnotations.initMocks(this);
    }
    @Test
    public void test1() {
        initMocks();
        myObject.doThis();
    }
}

运行此测试的输出为:[你好,世界]

如何确保两个向量都已正确分配?

我找不到任何关于将@Qualifier与Spy和InjectMocks一起使用的示例或文档。

我从基类调用super((,这是不正确的。由于成员是 @Autowire-d,因此不需要 super((。

相关内容

  • 没有找到相关文章

最新更新