Я сгенерировал тестовый пример ниже, используя инструментальный тестовый код codepro junit. Здесь я хотел вернуть фиктивный результат вызова метода getSum () класса Codetest2 из getSum () из класса CodeTest, но тестовый случай вызывает фактический метод (getSum ()) Codetest2) вместо того, чтобы возвращать Mockito.when () фиктивный результат. Может ли кто-нибудь помочь мне от этого. Спасибо заранее.
public class CodeTest {
CodeTest2 code2=new CodeTest2 ();
public int getSum(int a, int b){
int sum=code2.getSum2(a, b);
// return (a+b);
return sum;
}
}
public class CodeTest2 {
public int getSum2(int a, int b){
System.out.println("CodeTest2 ");
return (a+b);
}
}
public class CodeproJunitTest {
@InjectMocks
CodeTest fixture = new CodeTest ();
@Test
public void testGetSum_1()
throws Exception {
CodeTest fixture2 = Mockito.spy(new CodeTest ());
fixture2.code2 = Mockito.spy(new CodeTest2 ());
int a = 1;
int b = 1;
int sum=2;
when(fixture2.code2.getSum2(a,b)).thenReturn(sum);
int result = fixture2.getSum(a, b);
assertEquals(2, result);
}
@Before
public void setUp()
throws Exception {
}
@After
public void tearDown()
throws Exception {
}
public static void main(String[] args) {
new org.junit.runner.JUnitCore().run(CodeproJunitTest.class);
}
}