Junit 如何从StrutsTeseCase向struts操作注入模拟依赖项?

Junit 如何从StrutsTeseCase向struts操作注入模拟依赖项?,junit,struts2,mocking,mockito,Junit,Struts2,Mocking,Mockito,我正在编写一个从strutsspringtestcase扩展而来的测试用例,并调用以下操作: public class LoginActionTest extends StrutsSpringTestCase { @Mock DepenencyClass mockedObject; @InjectMocks LoginAction loginAction; @Before public void setUp() throws Ex

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

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

}
这是登录操作

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方法,而不是原始方法。在实现这一目标方面肯定存在差距,但任何帮助都将受到感谢

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

在有问题的地方发布一些代码。创建mock对象并在测试中使用mock-s的一些测试上下文。我创建了mock,用于从StrutsSpringTestCase扩展而来的单元测试中的依赖类。当我执行操作时,它不是调用存根方法,而是调用原始方法;并像when(mockedClass.method1())那样创建存根;并使用StrutsTestCase的execute()调用该操作。