如何通过JUnit& amp;测试这种方法EasyMock的?

时间:2016-05-12 16:03:52

标签: spring-mvc junit4 easymock

我有一个

@Component
public class MyBean{

         @Autowired
         Bean1 bean1;

         @Autowired
         Bean2 bean2;

         public void create(Param param1, Param param2){
           SomeObject object =    bean2.getDesiredResult();
         }

}

其中Bean2.java的{​​{1}}为instance variables -

autowired

我必须测试这个方法,

class Bean2{
    @Autowired
    Bean3 bean3;

    @Autowired
    Bean4 bean4;

    @Autowired
    Bean5 bean5;

    public Object getDesiredResult(){
        // some code which calls method on some beans which have autowired
        // beans, and this goes on and on further.
    }

}

主要问题是我继续得到这些除外:

  

没有类型为
的限定bean   无法自动装载字段

因为我无法手动create(Param param1, Param param2) 所有包,因为它们的数量太大了。项目中有大约3000个java包

component-scan

我正在使用<context:component-scan base-package &amp; JUnit框架。 请建议。

1 个答案:

答案 0 :(得分:0)

你似乎在混合两件事。您想要使用JUnit和EasyMock进行单元测试。这不需要Spring或任何autowire。您将执行以下操作:

// Record the mock
Bean2 mock = createMock(Bean2.class);
expect(mock.getDesiredResult()).andReturn(new SomeObject());
replay(mock);

// Configure the tested class
MyBean testSubject = new MyBean();
testSubject.setBean(mock);

// Test
testSubject.create(new Param1(), new Param2());

// Check the mock was called as expected
verify(mock);

用于包扫描。从我的观点来看,这与测试无关,包扫描可以是递归的。

相关问题