2

I have a method that I have to test -

public class DateUtil {

        public static Date getPreviousDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }
}

This code is part of Spring Boot.

The POM file is given below -

Pastebin for POM

I want to try mocking it using PowerMockRunner (as mentioned in many resources online) But I cannot seem to import it into my test file.

I tried mocking Mockito but that is also throwing error

Prateek Narendra
  • 1,837
  • 5
  • 38
  • 67
  • check [this](https://stackoverflow.com/a/36472441/6424383) – gokhanbirincii Sep 19 '18 at 13:15
  • @gokhanbirincii tried them all. didn't work in my project because somehow I cannot import powermockrunner, even if its specified in POM. Check the POM – Prateek Narendra Sep 19 '18 at 13:17
  • some mockito versions not compatible with powermockito's some versions, you can check versions in [here](https://github.com/powermock/powermock/wiki/Mockito#supported-versions) – gokhanbirincii Sep 19 '18 at 13:22
  • @gokhanbirincii tried the latest version by omitting what comes through spring-boot-starter-test and importing the versions speified in your document. Still same error – Prateek Narendra Sep 19 '18 at 13:39
  • Can you show us the test file and the error that is being thrown? – g00glen00b Sep 19 '18 at 13:45
  • There is no powermockito library in reference ni Ecipse despite mentioned in POM. So i cant even compile with the solutions shared here - https://stackoverflow.com/a/36472441/6424383 – Prateek Narendra Sep 19 '18 at 13:49
  • you can override default mockito version like: `version here` add this into properties tag. Just for proposal you can follow this steps : 1- put into properties compatible version in properties tag. 2- then add this dependencies – gokhanbirincii Sep 19 '18 at 14:07
  • 0) You mention Spring Boot. What impact does it have on testing of this simple utility class) 1) what are you trying to test? Whether java.util.Calendar works fine? It does. 2) There are several methods for testing such code (using PowerMock is one of them, redesigning it to use dependency injection is another). Can you redesign this class or do you have to test it as it is (but first, respond to question 1, please). – Tomek Kaczanowski Sep 19 '18 at 14:09
  • 2-` org.powermock powermock-module-junit4 ${powermock.version} test org.powermock powermock-api-mockito2 ${powermock.version} test ` – gokhanbirincii Sep 19 '18 at 14:12
  • @gokhanbirincii it didnt work – Prateek Narendra Sep 19 '18 at 14:56
  • [this](https://pastebin.com/YMPurtf8) should work. i am currently using this versions. – gokhanbirincii Sep 19 '18 at 15:05
  • @gokhanbirincii i see eclipse actually doesn't detect the package. I'm getting a new error now. I'll paste the stack trace in the morning – Prateek Narendra Sep 19 '18 at 20:04
  • @PrateekNarendra okey – gokhanbirincii Sep 20 '18 at 11:34
  • @gokhanbirincii looks like an eclipse issue. – Prateek Narendra Sep 20 '18 at 12:50

0 Answers0