0

As the title describes I would like to mock 3 getters of my class for testing purposes as the variables are only available when the program is in operation and a connection is established.

Using JUnit5 my BeforeEach setup:

@BeforeEach
void setUp() {
    dataStore = new DataStore();
    when(dataStore.getSecurityPrincipal()).thenReturn("CREDENTIALS");
    when(dataStore.getSecurityCredentials()).thenReturn("CREDENTIALS");
    when(dataStore.getProviderUrl()).thenReturn("CREDENTIALS");
}

But I want all the other methods of this class to remain the same, I thought about doing something like when(dataStore.getExample(anyString(), anyString())).thenCallRealMethod() for each method but there are a lot of methods and that would mean a lot of extra lines of code. Is there a better way to achieve this?

nixon
  • 41
  • 6
  • 1
    You can use spy instead of mock, anything you don't spy would call the original method without you specifying it – Matteo NNZ Aug 19 '22 at 16:17
  • Yes! Thank you that is exactly what I was looking for. This is going to be so useful for unit testing. – nixon Aug 19 '22 at 16:31

0 Answers0