admin管理员组

文章数量:1124387

I am struggling to mock a void method which is written inside the inject mocked class which i need to write junit for.

Maths.java

@Component
public class Maths {

@Autowired
private Data data;

  public String getData(){
    ////Some code block
    String value = data.someMethod();
    addTest("test");
    return value;
  }

  public void addTest(String text){
    ////Some code block
  }
}

MathsTest.java

@RunWith(MockitoJUnitRunner.class)
public class MathsTest{

  @InjectMocks
  private Maths maths;

  @Mock
  private Data data;

  @Test
  public void testGetData(){
     Mockito.when(data.someMethod).thenReturn("someValue");
     assertNotNull(maths.getData());
  }
}

Now in the above test case when maths.getData() is called I want to mock or ignore the code block addTest(). I have tried using @Spy on Maths class but that completely ignore the getData() method. If i use doNothing() then since the Maths class is not mocked, i get stubbing error. Requesting help as of how to achieve this?

本文标签: unit testingMocking a void method inside inject mock classStack Overflow