0

我想將模擬對象注入到測試bean的方法字段中。 我們有。像:將模擬對象注入方法字段

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

這豆,我用這種方式正在測試:

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

很可能沒有TestedBean代碼進行任何更改嘲笑ClassIWantToMock()?

回答

0

你是指真正的模擬框架,如Mockito?或者你想要達到/測試什麼?

對你的問題的簡單答案是否定的。只要您直接在已測試方法中實例化實例,則無法模擬它。

難道你不能注入類,但當然你需要改變代碼。取決於你想測試一個像Byteman這樣的庫可以提供什麼幫助,特別是如果你試圖測試錯誤情況。