Skip to content
Advertisement

Tag: mockito

Junit test for model class

This question is about best practice rather than any issues or problem. I have a service method below that I am trying to test. myDAO is DAO class that will be injected and has all database call code. And my Junit test case using mockito is Everything is fine and working. My main problem here is MyObject is a modal

JUnitParams with Mockito

This could be a XY problem but I want to ask: I am using JUnitParams to be able to run my test method 10 times with different objects. The problem is that the injections don’t work (@Mock and @InjectMocks). I can use to mock the PersonService in the @Test method. But how do mock the “main” object that is annotated

How to mock a Files static method with Mockito

I have a method in my utilities: I want to mock Files so I can test to make sure Files.createDirectories with the expected fields are called or not. Can I do this with Mockito? Or do I need to actually create the directories and check for their existence in some tmp folder? Answer When you write something with tdd and

Testing method called with specific string

I am trying to find answer to my question but unable to find something working. I have a class which does this and my test is How would I test that calling llw.logInfo actually calls lambdaLogger.log with the appropriate string? Answer Invoke the logInfo() method on the instance under test : And use the Mockito#verify() method to assert that the

Junit Mockito error on initialization

I have looked everywhere online. I have tried almost everything, from deleting STS to deleting all the code from my station and re-import everything back. I played with dependencies, build path, properties, etc. but still nothing works. Running the tests on Maven by CLI is working fine, and other team members don’t have that issue, so it is probably some

Creating strict mock when using @MockBean of spring boot?

I use @MockBean of spring boot (with @RunWith(SpringRunner.class)) and everything was ok so far. However the mock provides default implementation for every method of the mocked class so I cannot check if only those methods were called that I expected to be called, i.e. I would like to create strict mock. Is that possible with @MockBean? I don’t insist on

Mock final class with Mockito 2

I’m removing Powermock from the project I’m currently working on, so I’m trying to rewrite some existing unitary test only with Mockito (mockito-core-2.2.28). When I run the test, I have the following error: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.ExternalpackagePath.Externalclass Mockito cannot mock/spy because : final class I know that this question has already been asked (How to mock a final class

Advertisement