Mockito's Mock Methods | Baeldung
www.baeldung.com › mockito-mock-methodsJul 31, 2022 · The simplest overloaded variant of the mock method is the one with a single parameter for the class to be mocked: public static <T> T mock(Class<T> classToMock) We'll use this method to mock a class and set an expectation: MyList listMock = mock(MyList.class); when(listMock.add(anyString())).thenReturn(false); Then we'll execute a method on the mock:
java - Test class with a new() call in it with Mockito ...
stackoverflow.com › questions › 5920153Test class with a new () call in it with Mockito. I have a legacy class that contains a new () call to instantiate a LoginContext object: public class TestedClass { public LoginContext login (String user, String password) { LoginContext lc = new LoginContext ("login", callbackHandler); } } I want to test this class using Mockito to mock the LoginContext as it requires that the JAAS security stuff be set up before instantiating, but I'm not sure how to do that without changing the login () ...