How to use @InjectMocks and initMocks() with an object that has a required String parameter?

I think the simple answer is not to use @InjectMocks, and instead to initialise your object directly. The only downside I can see is that you’re not testing the injection, but then with @InjectMocks, I think you’d be testing it with Mockito’s injection implementation, rather than your real framework’s implementation anyway, so no real difference. … Read more

Mockito.mockedStatic for method with arguments

Edit – Mockito 3.7.7 Mockito 3.7.7 unified order of verify parameters (Issue #2173) Updated code: try (MockedStatic<Foo> dummyStatic = Mockito.mockStatic(Foo.class)) { dummyStatic.when(() -> Foo.method(“param1”)) .thenReturn(“someValue”); // when System.out.println(Foo.method(“param1”)); //then dummyStatic.verify( () -> Foo.method(“param1”), times(1), ); } Original answer It is possible, you need to use a lambda instead of a method reference: try (MockedStatic<Foo> dummyStatic … Read more

Should you only mock types you own?

My answer is “no”. You should mock anything that makes sense in the context of a given unit test. It should not matter if you “own” the mocked type or not. These days, in a Java or .NET environment everything (and I really mean everything) can be easily mocked. So, there is no technical reason … Read more

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