I run into an issue that, if I mock a class instance, then a static instance in the same class is not initialized correctly. I have a real example of third party code with relevant to the question lines: Then the project jar is used in another project where Schema is mocked in a test: To my understanding this…
Tag: mockito
Is it possible to retrieve value from application.properties without autowiring the class under test?
I have a test class that is annotated with @Spy and @InjectMocks and tested using Mockito. The class under test has a value (url) that is retrieved from the application.properties file. I’d like to test whether this url is being set correctly within the method that uses it. I can do this if I remove the…
Spring Boot 2.6.4 -> 2.6.6 : strange NullPointerException within Logback when logging a mock Exception
while upgrading from Spring Boot 2.6.4 to 2.6.6 , one of my tests (written in Kotlin), fails : the build passes with Spring Boot 2.6.4. It works in Spring Boot 2.6.6 when I run the test individually in my IDE, but fails during the maven build. the stacktrace was not showing by default, but after surrounding t…
How create unit test in Spring REST in POST Cotroller
I have a Spring Boot Controller with POST. Method works fine. I tested it by POSTMAN and from postgresql I recieved JSON. But I need test it. I created test: when I run a test, I received error like below: What am I doing wrong? Answer The problem is this line: Right now you are sending the object in the
Mockito is returning the incorrect response when testing a controller
I Have a misterius. Im trying to write a Test for this controller Method: And I DID this: I don’t know why but the method ResponseEntity<Object> response = controller.getIncidencia(ID); is returning <404 NOT_Found Not Found, Incidencia Not Found []> But the corret would be 200 OK. What I did…
How to mock private method using powermock?
If I have a simple class like: What modification do I need to mock the private method getDefaultLuckyNumber? I have this which doesnt work Answer PowerMock annotations required for mock working: Example of working test: PowerMock version:
NullPointerException Mock when use RestTemplate
I am new to using mockito and I have a problem. I wrote a controller test and it uses restTemplate to get data from another api, but when I run the test, the variable of this type is null. In result NullPointerException. What could be the problem? Controlle: Test: Answer This is because you don’t have r…
How to ignore TimeUnit.SECONDS.sleep in unit tests
I have a method which is calling sleep of 30 seconds but I dont want my unit test to have the same. I want my unit test to ignore the sleep and execute the rest on the method or reduce the sleep time to 1 sec. From the above sample code, How do I ignore the TimeUnit.SECONDS.sleep(30) when executing the
Java how to test correctly mocked instances of POI Row and Cell when use iterator forEach()
Given this code: I can test it with this: But, if I want to use forEach() instead of for loop in getRowAsString() method, like this: The test doesn’t work because when use stream.forEach() the code is not calling method getCell(index). How should I test the method to be able to use forEach() instead of …
Mockito when() in parallel stream unexpected result
I was investigating Mockito’s behavior in one of our tests and I didn’t understand its behavior. The following code snippets shows the same behavior : The output I was expecting is prints of 1,2,3 in some order (not nessaseraly sorted) : The output I received : Why I got this output ? Answer This …