2012-04-07 2 views
4

EasyMock과 EasyMock CE 3.0을 사용하여 종속 레이어를 모방하고 클래스를 테스트하고 있습니다. 다음은 어떤 솔루션을 찾을 수없는 시나리오입니다.EasyMock을 사용하여 void 메쏘드에 전달 된 params에 기대치를 설정하는 방법

종속 클래스를 호출하는 void 메서드는 입력 매개 변수를 사용하고 동일한 매개 변수를으로 변경합니다. 내가 테스트입니다 방법은 내가 다양한 시나리오

지금 테스트해야 변경된 PARAM에 따라 몇 가지 작업을하고있다 나도 같은 시나리오를 넣어 시도 아래의 샘플을 고려

public boolean voidCalling(){ 
    boolean status = false; 
    SampleMainBean mainBean = new SampleMainBean(); 
    dependentMain.voidCalled(mainBean); 
    if(mainBean.getName() != null){ 
     status = true; 
    }else{ 
     status = false; 
    } 
    return status; 
} 

그리고 dependentMain 클래스는 아래의 방법

public void voidCalled(SampleMainBean mainBean){ 
    mainBean.setName("Sathiesh"); 
} 

전체 범위를 가지려면, 나는 모두 테스트하기 위해이 테스트 케이스를 가질 필요가 참되고 거짓이 반환 시나리오,하지만 난 설정할 수 없습니다 이대로 항상 false를 얻을 이 입력을 변경하는 void 메서드의 동작 콩. 나는 어떤 도움을 사전에 EasyMock에

에게

감사를 사용하여이 시나리오에서 결과로 진실을 얻을 수있는 방법.

답변

6

이 답변의 대답에 시작 : EasyMock: Void Methods을, 당신은 IAnswer를 사용할 수 있습니다. 답장을 보내

// create the mock object 
DependentMain dependentMain = EasyMock.createMock(DependentMain.class); 

// register the expected method 
dependentMain.voidCalled(mainBean); 

// register the expectation settings: this will set the name 
// on the SampleMainBean instance passed to voidCalled 
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { 
    @Override 
    public Object answer() throws Throwable { 
     ((SampleMainBean) EasyMock.getCurrentArguments()[0]) 
       .setName("Sathiesh"); 
     return null; // required to be null for a void method 
    } 
}); 

// rest of test here 
2

덕분에 .. 나는 문제가 너무 ... :) 샘플 코드에 대한 감사를 해결되었다. 방법에서 업데이트 된 콩을 얻을 수이 오전으로 내가해야 할 일을했을 하나의 변화는

위의 코드를 사용하여,

// register the expected method 
dependentMain.voidCalled((SampleMainBean) EasyMock.anyObject()); 

테스트 할 수 있습니다.

다시 도움 주셔서 감사합니다.

관련 문제