0

I want to inject mock object into method's fields in testing bean. We have sth. like:

@Stateless
@LocalBean
public class TestedBean implements TestedBeanInterface
{
  public OtherClass testedMethod { 
     private ClassIWantToMock necessaryField = new ClassIWantToMock(); 
     return necessaryField.doThingsImpossibleToDoAtTest(); 
  }  
}

And that bean, I am testing in this way:

@RunWith(Arquillian.class)
public class TripPlannerFactoryBeanTest {

   @Deployment
   public static JavaArchive createDeployment() {
      //
   }

   @EJB(mappedName = "java:module/TestedBean!ab.abc.abcd.TestedBean")
   TestedBean testedBean;

   @Test
   public void testMethodOfTestedBean(){
   testedBean.testedMethod();
   }
}

It is possible to mock ClassIWantToMock() without any changes in TestedBean code?

4

1 回答 1

0

您指的是像 Mockito 这样的实际模拟框架吗?或者你到底想达到/测试什么?

您的问题的简单答案是否定的。只要您直接在testMethod中实例化实例,就无法模拟它。

您可以不注入该类,但是您当然需要更改代码。根据您想要测试的内容,像 Byteman 这样的库可以提供帮助,尤其是在您尝试测试错误案例时。

于 2013-07-31T08:21:53.687 回答