2016-12-12 4 views
0

클래스 생성자를 처음 PowerMockito로 모의하려고하지만 작동하지 않습니다. 현재 코드 :PowerMockito를 사용하여 생성자를 모의 처리하는 방법

public class Bar { 
    public String getText() { 
     return "Fail"; 
    } 
} 

public class Foo { 
    public String getValue(){ 
     Bar bar= new Bar(); 
     return bar.getText(); 
    } 

} 

@RunWith(PowerMockRunner.class) 
@PrepareForTest(Bar.class) 
public class FooTest { 
    private Foo foo; 
    @Mock 
    private Bar mockBar; 

    @Before 
    public void setUp() throws Exception { 
     MockitoAnnotations.initMocks(this); 
     PowerMockito.whenNew(Bar.class).withNoArguments().thenReturn(mockBar); 
     foo= new Foo(); 
    } 

    @Test 
    public void testGetValue() throws Exception { 
     when(mockBar.getText()).thenReturn("Success"); 
     assertEquals("Success",foo.getValue()); 

    } 
} 

반환 값이 "실패"이기 때문에 테스트가 실패합니다. 내 문제는 어디에 있습니까?

답변

1

좋아요는 답을 발견, 당신은

@PrepareForTest(Foo.class) 

대신

@PrepareForTest(Bar.class) 
에 호출 할 필요가