java.lang.IllegalStateException: Failed to load ApplicationContext error java spring-boot -


okay went through lot of answers problem still unsolved. trying test controller -

@controller @produces(mediatype.application_json) @requestmapping(value = "/verify/email") public class emailcontroller {  @autowired private byemail strategy;  @autowired private applicationconfig config;  @responsebody @requestmapping(method = requestmethod.post, produces =  mediatype.application_json) public request newrequest(@requestbody vrequest request) throws  ioexception {     log.info(request.tostring());     system.out.println(request.tostring());     request.settype(vtype.email);     optional<request> response = strategy.processrequest(request);     return response.orelsethrow(badrequestexception::new); } 

and test -

@runwith(springrunner.class) @autoconfiguremockmvc @webappconfiguration @springboottest public class testemailcontroller {  @mock private byemail strategy;  @injectmocks @autowired private emailcontroller controller;  @autowired private mockmvc mockmvc;  @test public void contexloads() throws exception {     assertthat(controller).isnotnull(); } 

when tried run test giving following error -

java.lang.illegalstateexception: failed load applicationcontext  @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:124) @ org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:83) @ org.springframework.test.context.web.servlettestexecutionlistener.setuprequestcontextifnecessary(servlettestexecutionlistener.java:189) @ org.springframework.test.context.web.servlettestexecutionlistener.preparetestinstance(servlettestexecutionlistener.java:131) @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:230) @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:228) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:287) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:289) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:247) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:94) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:191) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:68) @ com.intellij.rt.execution.junit.ideatestrunner$repeater.startrunnerwithargs(ideatestrunner.java:47) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:242) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:70)  caused by: org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'application': unsatisfied dependency expressed through field 'configs'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'applicationconfig': not bind properties applicationconfig (prefix=company.services.verification, ignoreinvalidfields=false, ignoreunknownfields=true, ignorenestedproperties=false); nested exception org.springframework.validation.bindexception: org.springframework.boot.bind.relaxeddatabinder$relaxedbeanpropertybindingresult: 2 errors field error in object 'company.services.verification' on field 'redirecturl': rejected value [null]; codes [notnull.company.services.verification.redirecturl,notnull.redirecturl,notnull.java.lang.string,notnull]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [company.services.verification.redirecturl,redirecturl]; arguments []; default message [redirecturl]]; default message [may not null] field error in object 'company.services.verification' on field 'countries': rejected value [null]; codes [notnull.company.services.verification.countries,notnull.countries,notnull.java.util.list,notnull]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [company.services.verification.countries,countries]; arguments []; default message [countries]]; default message [may not null]     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:588)     @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:88)     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:366)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1264)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:553)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483)     @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306)     @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230)     @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302)     @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:761)     @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:867)     @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:543)     @ org.springframework.boot.springapplication.refresh(springapplication.java:693)     @ org.springframework.boot.springapplication.refreshcontext(springapplication.java:360)     @ org.springframework.boot.springapplication.run(springapplication.java:303)     @ org.springframework.boot.test.context.springbootcontextloader.loadcontext(springbootcontextloader.java:120)     @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:98)     @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:116)     ... 24 more caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'applicationconfig': not bind properties applicationconfig (prefix=company.services.verification, ignoreinvalidfields=false, ignoreunknownfields=true, ignorenestedproperties=false); nested exception org.springframework.validation.bindexception: org.springframework.boot.bind.relaxeddatabinder$relaxedbeanpropertybindingresult: 2 errors field error in object 'company.services.verification' on field 'redirecturl': rejected value [null]; codes [notnull.company.services.verification.redirecturl,notnull.redirecturl,notnull.java.lang.string,notnull]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [company.services.verification.redirecturl,redirecturl]; arguments []; default message [redirecturl]]; default message [may not null] field error in object 'company.services.verification' on field 'countries': rejected value [null]; codes [notnull.company.services.verification.countries,notnull.countries,notnull.java.util.list,notnull]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [company.services.verification.countries,countries]; arguments []; default message [countries]]; default message [may not null]     @ org.springframework.boot.context.properties.configurationpropertiesbindingpostprocessor.postprocessbeforeinitialization(configurationpropertiesbindingpostprocessor.java:334)     @ org.springframework.boot.context.properties.configurationpropertiesbindingpostprocessor.postprocessbeforeinitialization(configurationpropertiesbindingpostprocessor.java:291)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applybeanpostprocessorsbeforeinitialization(abstractautowirecapablebeanfactory.java:409)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1620)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:555)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483)     @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306)     @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230)     @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302)     @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:202)     @ org.springframework.beans.factory.config.dependencydescriptor.resolvecandidate(dependencydescriptor.java:208)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1138)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1066)     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:585)     ... 42 more caused by: org.springframework.validation.bindexception: org.springframework.boot.bind.relaxeddatabinder$relaxedbeanpropertybindingresult: 2 errors field error in object 'company.services.verification' on field 'redirecturl': rejected value [null]; codes [notnull.company.services.verification.redirecturl,notnull.redirecturl,notnull.java.lang.string,notnull]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [company.services.verification.redirecturl,redirecturl]; arguments []; default message [redirecturl]]; default message [may not null] field error in object 'company.services.verification' on field 'countries': rejected value [null]; codes [notnull.company.services.verification.countries,notnull.countries,notnull.java.util.list,notnull]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [company.services.verification.countries,countries]; arguments []; default message [countries]]; default message [may not null]     @ org.springframework.boot.bind.propertiesconfigurationfactory.checkforbindingerrors(propertiesconfigurationfactory.java:359)     @ org.springframework.boot.bind.propertiesconfigurationfactory.dobindpropertiestotarget(propertiesconfigurationfactory.java:276)     @ org.springframework.boot.bind.propertiesconfigurationfactory.bindpropertiestotarget(propertiesconfigurationfactory.java:240)     @ org.springframework.boot.context.properties.configurationpropertiesbindingpostprocessor.postprocessbeforeinitialization(configurationpropertiesbindingpostprocessor.java:329)     ... 55 more 

this applicationtest class -

 @runwith(springrunner.class)     @springboottest     public class applicationtests {      @test     public void contextloads() {     }  } 

here applicationconfig class -

@data @configuration @enableautoconfiguration @configurationproperties(prefix = "company.services.verification") public class applicationconfig {  @notnull string smstype;  @notnull string fromemail;  @notnull string subjectemail;  @notnull string subjectmanual;  @notnull list<integer> countries;  @notnull string redirecturl; } 

and application.yaml file abover properties defined -

company:     services:       verification:         smstype: claims         fromemail: noreply@company.com         subjectemail: email verification         subjectmanual: claim initiated         redirecturl: http://www.google.com         countries:          - 1          - 2          - 3          - 4 

i went through lot of answers error remains same. using spring-boot 1.5.6. please?

first of all, please add following dependency project , follow spring doc instructions "generating own meta-data using annotation processor"

http://docs.spring.io/spring-boot/docs/1.5.6.release/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor

here maven dependency generating custom spring meta-data configuration properties

<dependency>     <groupid>org.springframework.boot</groupid>     <artifactid>spring-boot-configuration-processor</artifactid>     <optional>true</optional> </dependency> 

your applicationconfig class should have getters , setters

@configuration @enableautoconfiguration @configurationproperties(prefix = "company.services.verification") public class applicationconfig {      @notnull     string smstype;      @notnull     string fromemail;      @notnull     string subjectemail;      @notnull     string subjectmanual;      @notnull     list<integer> countries;      @notnull     string redirecturl;      public string getsmstype() {         return smstype;     }      public void setsmstype(string smstype) {         this.smstype = smstype;     }      public string getfromemail() {         return fromemail;     }      public void setfromemail(string fromemail) {         this.fromemail = fromemail;     }      public string getsubjectemail() {         return subjectemail;     }      public void setsubjectemail(string subjectemail) {         this.subjectemail = subjectemail;     }      public string getsubjectmanual() {         return subjectmanual;     }      public void setsubjectmanual(string subjectmanual) {         this.subjectmanual = subjectmanual;     }      public list<integer> getcountries() {         return countries;     }      public void setcountries(list<integer> countries) {         this.countries = countries;     }      public string getredirecturl() {         return redirecturl;     }      public void setredirecturl(string redirecturl) {         this.redirecturl = redirecturl;     } } 

emailcontroller.class

@controller @produces(mediatype.application_json) @requestmapping(value = "/verify/email") public class emailcontroller {      @autowired     private applicationconfig config;      @responsebody     @requestmapping(method = requestmethod.post, produces = mediatype.application_json)     public string newrequest(@requestbody string email) throws             ioexception {          return email;     } } 

since dont have idea byemail class in controller removed it.

application.yaml

company:   services:     verification:       from-email: noreply@company.com       redirect-url: http://www.google.com       subject-email: email verification       smstype: claims       subject-manual: claim initiated       countries:         - 1         - 2         - 3         - 4 

and here important part, should add applicationcontext test classes , make necessary mocking operations before test.

emailcontrollertests.java

@runwith(springrunner.class) @autoconfiguremockmvc @webappconfiguration @springboottest public class emailcontrollertests {     @autowired     private mockmvc mockmvc;      @autowired     private webapplicationcontext applicationcontext;      @mock     private applicationconfig applicationconfig;      @before     public void init() throws exception {         mockitoannotations.initmocks(this);          // @formatter:off         this.mockmvc = webappcontextsetup(this.applicationcontext)                 .alwaysdo(print())                 .build();         // @formatter:on          emailcontroller emailcontroller = this.applicationcontext.getbean(emailcontroller.class);          // replace services mocked ones here         //reflectiontestutils.setfield(emailcontroller, "byemail", this.byemail);         reflectiontestutils.setfield(emailcontroller, "config", this.applicationconfig);     }      @test     public void unauthenticateduserscannotaddproperty() throws exception {         // @formatter:off         mvcresult result = this.mockmvc.perform(post("/verify/email").contenttype(mediatype.application_json).content("{ahmet@ahmet.com}"))                 .anddo(print())             .andexpect(status().isok())             .andreturn();         // @formatter:on          assert.assertnotnull(result);     } } 

and here result, working should be...

enter image description here

hope helps...


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -