Mock static method in JUnit 5 using Mockito

You need to use mockito-inline version 3.4.0 or higher and remove mockito-core from your dependencies (mockito-inline depends on mockito-core so it will be retrieved automatically). That way, you will be using mockito static mocking correctly, but it wouldn’t solve the exception that you posted. To fix it, you need to remove all dependencies of powermock … Read more

Mockito + Spy: How to gather return values

First thing, you should be passing spy in as the constructor argument. That aside, here’s how you could do it. public class ResultCaptor<T> implements Answer { private T result = null; public T getResult() { return result; } @Override public T answer(InvocationOnMock invocationOnMock) throws Throwable { result = (T) invocationOnMock.callRealMethod(); return result; } } Intended … Read more

mockito ArrayList problem

The alternative is to use the @Mock annotation since then Mockito can use type reflection to find the generic type: public class MyTest { @Mock private ArrayList<String> mockArrayList; … public void setUp() { MockitoAnnotations.initMocks(this); } public void testMyTest() { when(mockArrayList.get(0)).thenReturn(“Hello world”); String result = mockArrayList.get(0); assertEquals(“Should have the correct string”, “Hello world”, result); verify(mockArrayList).get(0); } … Read more

Mockito verify() fails with “too many actual invocations”

It looks like you both want to mock what happens when userService.getUserById() is called, and also verify that setPasswordChangeRequired(true) is called on that returned object. You can accomplish this with something like: UserService userService = mock(UserService.class); User user = mock(User.class); when(userService.getUserById(anyLong())).thenReturn(user); … // invoke the method being tested … verify(user).setPasswordChangeRequired(true);

Mock class in class under test

You could refactor MyClass so that it uses dependency injection. Instead of having it create an AnythingPerformerClass instance you could pass in an instance of the class to the constructor of MyClass like so : class MyClass { private final AnythingPerformerClass clazz; MyClass(AnythingPerformerClass clazz) { this.clazz = clazz; } public boolean performAnything() { return clazz.doSomething(); … Read more

Mockito UnfinishedStubbingException

From what I read on “Issue 53” of mockito (https://code.google.com/p/mockito/issues/detail?id=53) , my code was experiencing a problem due to the validation framework involved in Mockito. Precisely the following code was causing the exception per se. private ConstantNode getConstantNode(NumericalValue value){ ConstantNode node = Mockito.mock(ConstantNode.class); Mockito.when(node.evaluate()).thenReturn(value); Mockito.when(node.toString()).thenReturn(value.toString()); return node; } If you remember from my code, the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)