spring boot - How to mock application properties annotation with powermock? -


i have problem powermock in spring boot project.

my application.yml this:

app:   cache:    config:      connection-time-out: 1000      time-expired: 10000    name: "cachename" 

here applicationprops.java:

@configurationproperties(prefix = "app", ignoreunknownfields = false) public class applicationprops {    private final cache cache = new cache();    public cache getcache() {       return cache;    }     public static class cache() {        private string name = "";        private final config config = new config();         public string getname() {            return name;        }        public void setname(string name) {            this.name = name;        }        public config getconfig(){            return config;        }         public static class config() {            int connectiontimeout;            int timeexpired;            public int gettimeexpried() {                return timeexpried;            }             public void settimeexpried(int timeexpried) {                this.timeexpried = timeexpried;            }             public int getconnectiontimeout() {                return connectiontimeout;            }             public void setconnectiontimeout(int connectiontimeout) {                this.connectiontimeout = connectiontimeout;            }        }    } } 

cacheconfig.java:

@configuration public class cacheconfig() {     @autowired     private applicationprops applicationprops;      public void init() {         string name = applicationprops.getcache().getname();         int timeexpired = applicationprops.getcache().getconfig().gettimeexpried();     } } 

and cacheconfigtest.java

@runwith(powermockrunner.class) public class sqsconfigtest {      @mock     applicationprops applicationprops;      @injectmocks     cacheconfig cacheconfig;      @test     public void inittest() {         cacheconfig.init();     } } 

when run jnuit test throw nullexception, it's cannot mock properties in applicationprops , it's allways null.

anyone have solution error. can powermock mock application proporties?

thanks all!

as mentioned in comments, reason you're getting nullpointerexception because mocking framework use, mocks applicationprops. means not have behaviour on own, means methods return null.

in cacheconfig's init() method have:

applicationprops.getcache().getconfig().gettimeexpried(); 

since applicationprops.getcache() mocked, return null. however, since you're calling getconfig() on top of that, throw nullpointerexception you're seeing.

the solution mock calls. if want make sure timeexpired correctly set up, need return when applicationprops.getcache() called. way depends on mocking framework (powermock can integrate mockito, easymock, ...).

when use powermockito, do:

when(applicationprops.getcache()).thenreturn(...); 

in test want return new cache object, config object in given timeexpired value.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -