如何将模拟的依赖项注入StrutsTeseCase的struts操作?

时间:2015-04-11 15:14:30

标签: junit struts2 mocking mockito

我正在编写一个测试用例,从strutsspringtestcase扩展并调用以下操作:

这是登录操作

public class LoginAction extends ActionSupport
{
    @Autowired(required = true)
    @Qualifier("DependencyClass")
    protected DependencyClass dependencyObject;

    public void execute()
    {
    return dependencyObject.method1();
    }
}

另外,我正在使用mockito创建模拟对象,如下所示:

   public class LoginActionTest extends StrutsSpringTestCase
   {
    @Mock
    DepenencyClass mockedObject;

    @InjectMocks
    LoginAction loginAction;

    @Before
     public void setUp() throws Exception{
     super.setUp();
     when(mockedObject.Method1()).thenReturn(result);
    }

    public void testLoginAction throws Exception{ 
    `ActionProxy proxy = getActionProxy("/login");
     LoginAction action = (LoginAction) proxy.getAction();
     String result = proxy.execute();`
    }

}

我的期望是该操作应该使用mocked方法而不是原始方法。实现这一点肯定存在差距,但任何帮助都应该受到赞赏。

1 个答案:

答案 0 :(得分:0)

您需要让您的动作类使用您的模拟对象。使用setter或@InjectMocks注释将该对象放入您的操作类。

相关问题