2014-11-03 6 views
0

Пожалуйста, помогите, это насмешка не работает:Невозможно издеваться частный метод с помощью JMockit

class ClassBeingTested { 

    private AnotherClass anotherClass; 

    public void someMethod() { 
     int ans = anotherClass.targetMethod(5); 
     // Use ans here 
    } 
} 

// Мой тест

ClassBeingTested classObject; 
AnotherClass anotherClassObject; 

@Before 
public void setup() { 
     // Initialize anotherClassObject here 

     classObject = new ClassBeingTested(anotherClassObject); 

     new NonStrictExpectations(anotherClassObject) {{ 
      invoke(anotherClassObject, "targetMethod", Integer.class); result = 100; 
     }}; 
} 

@Test 
public void testSomeMethod() { 
    classObject.someMethod(); 
} 

ответ

0

Mocking работал, как только я заменил Integer.class с фактическая ожидаемая стоимость int.

new NonStrictExpectations(anotherClassObject) {{ 
     invoke(anotherClassObject, "targetMethod", 5); result = 100; 
    }}; 
0

Как насчет этого способа насмешек над JMockit?

import mockit.Injectable; 
import mockit.Tested; 
... 


@Tested 
ClassBeingTested classObject; 
@Injectable 
AnotherClass anotherClassObject; 

@Before 
public void setup() { 
    new Expectations() {{ 
     anotherClassObject.targetMethod(anyInt); result = 100; 
    }}; 
} 

@Test 
public void testSomeMethod() { 
    classObject.someMethod(); 
} 
Смежные вопросы