I am using IntelliJ idea for writing simple unit test cases using JUnit and Mockito. I am using Maven for dependency management. IntelliJ idea keeps complaining that following imports cannot be resolved: Following is the dependencies section of my project: Following is my project structure: Answer Try View -> Tool Windows -> Maven projects, then click the blue icon in
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
Mockito anyListOf() List<List>
I’m using mockito-core:2.8.47 and Java 7 and want to use in a when and verify anyListOf or some other any method. My Problem is, if I just use anyList it says: The method name( int, List < List < String > >) in the type Y is not applicable for the arguments ( int, List < Object > ) How
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
Is there way to mock pojo other then setting values manually in mockito?
consider a method returns class A in my service, My pojo class so in test case ,i wrote as Here everything is working fine .Code coverage is also fine.But my question is ,is there any possible way to avoid above private method that returns class A by using mockito ? Answer in a unittest you define the behavior of your
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