jeudi 1 janvier 2015

Mocked method is never getting called

I'm trying to write a test case for a java class. I've mocked a method using Mockito, but the mock is never being used. Why?


Below is the structure of my java class:



class A {

@Autowired
private ClassB classB;

publiv void methodOne() {
methodTwo();
}

private void methodTwo() {
...
int returnedValue = classB.someMethod();
...
}
}


The test class is given below:



class ATest {

@Mock
private ClassB classB;

@InjectMocks
@Autowired
ClassA classA;

@Before
public void setupTest() {
MockitoAnnotations.initMocks(this);
}

@Test(expected = SomeException.class)
public void testMethodOne() {
when(classB.someMethod()).thenReturn(29);
classA.methodOne();
}
}


I've gone through the existing questions but have been unable to find an answer. If there is any question/answer that may help me, please point me to the same.


Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire