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



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

这是登录操作

public class LoginAction extends ActionSupport
{
    @Autowired(required = true)
    @Qualifier("DependencyClass")
    protected DependencyClass dependencyObject;
    public void execute()
    {
    return dependencyObject.method1();
    }
}

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

   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();`
    }
}

我的期望是该操作应该命中模拟方法而不是原始方法。在实现这一目标方面肯定存在差距,但任何帮助都应不胜感激。

你需要让你的操作类使用你的模拟对象。使用资源库或@InjectMocks批注将该对象放入操作类中。

最新更新