Java Easymock调用自动连线对象方法

Java Easymock调用自动连线对象方法,java,spring,testing,easymock,Java,Spring,Testing,Easymock,假设我有以下课程: public class A { @Autowired B b; public void doSomething(){ b.doSomeThingElse(); } @Component @Autowired C c; public class B { public void doSomethingElse(){ c.doIt(); } 当你知道我想模仿c.doIt,却想叫b.doSomethingElse时,我如何测试A;和EasyMock一起 提前感谢@自

假设我有以下课程:

public class A {
@Autowired B b;
public void doSomething(){
   b.doSomeThingElse();
}


@Component
@Autowired C c;
public class B {
public void doSomethingElse(){
  c.doIt();
}
当你知道我想模仿c.doIt,却想叫b.doSomethingElse时,我如何测试A;和EasyMock一起

提前感谢

@自动连线很好,但会让我们忘记如何测试。只需为b和c添加一个setter

或者使用构造函数注入

C c = mock(C.class);
c.doIt();

replay(c);

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

a.doSomething();

verify(c);
在这种情况下,您的类将成为:

public class A {
    private B b;
    public A(B b) { // Spring will autowired by magic when calling the constructor
        this.b = b;
    }
    public void doSomething() {
        b.doSomeThingElse();
    }
}

@Component
public class B {
    private C c;
    public B(C c) {
        this.c = c;
    }
    public void doSomethingElse(){
        c.doIt();
    }
}

谢谢但我使用了:ReflectionTestUtils.setField,但您的答案也可以,它也可以。我本来可以回答这个问题,但我真的不喜欢。在不必要时中断封装
public class A {
    private B b;
    public A(B b) { // Spring will autowired by magic when calling the constructor
        this.b = b;
    }
    public void doSomething() {
        b.doSomeThingElse();
    }
}

@Component
public class B {
    private C c;
    public B(C c) {
        this.c = c;
    }
    public void doSomethingElse(){
        c.doIt();
    }
}