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:
Tag: powermockito
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 traditional for loop? Answer Yes, I
How to mock LocalDateTime.now() in java 8
Please, I am testing a functionality ABC that uses LocalDateTime.now(). In the methode ABC i’am comparing an entry date with the LocalDateTime.now() I want my test to be passed at any day so i have to mock the LocalDateTime.now() This is my test: I am using JAVA 8 the date shown in the console is always the real LacalDateTime not
Handle anonymous class with Generics in Mockito
I am trying to write unit test using Powermockito for following method – I have written test method as – When I run this test I always get it failed saying – It looks like stub is not working as I always get “null” for this line – Is it because anonymous instantiation of TypeReference class? Answer Yes, it’s because