0
public class ClassOne {
  
  private String exampleString;
  
  public String getExampleString(){
    return this.exampleString;
  }
  
  public void setExampleString(String exampleString){
    this.exampleString;
  }
}
public class ClassOneTest {

  ClassOne classOne = new ClassOne();

  @Test
  void getExampleStringTest(){
    classOne.setExampleString("test");
    assertEquals("test", classOne.getExampleString());
  }
  
  @Test
  void setExampleStringTest(){
    ClassOne classOneMock = mock(ClassOneMock.class);
    ClassOneMock.setExampleString("test");
    verify(classOneMock, times(1));
  }

I have two methods, one which sets a variable and another which gets the value of that variable. I want to be able to test by mocking the set method and verifying that the value of the variable set is correct.

I tried using ArgumentCaptor, but I'm not sure if I understand it enough to use it.

Shakakhan
  • 41
  • 1
  • 1
  • 2
  • 1
    Does this answer your question? [Unit testing void methods?](https://stackoverflow.com/questions/246038/unit-testing-void-methods) – hfontanez Mar 18 '22 at 23:10
  • 4
    You don't, and shouldn't, test that you've set a private variable. Only test the public behavior of the class -- whatever's controlled _by_ that private variable. – Louis Wasserman Mar 18 '22 at 23:11
  • 3
    Most of the time it is useless to test your `getter/setter`. – Harry Coder Mar 18 '22 at 23:23
  • You already tested your `setExampleString` method, in your `getExampleStringTest`. if it didn't work, that test would fail. However, Louis Wasserman and Harry Coder are both absolutely correct. It's a waste of time to test getters and setters; and you should never test changes to the private state of a class - only the publicly accessible effects of such a change. – Dawood ibn Kareem Mar 21 '22 at 02:27

0 Answers0