Why I can't autowire a controller instance into a JUnit class of this Spring Boot application? Unable to find a @SpringBootConfiguration -


i working on first spring boot application , finding difficulties trying test controller class using junit.

so have following situation, controller class named placesearchercontroller:

@restcontroller @requestmapping("/placesearcher") public class placesearchercontroller {      private static final logger log = loggerfactory.getlogger(placesearchercontroller.class);       @autowired     private mainsearchservices mainsearchservices;      @requestmapping(value = "hello")     public string hello() {         return "hello world";     }      @requestmapping(value = "",             method = requestmethod.get)     public responseentity<string> home() {         list<accomodationdto> accomodationdtos = new arraylist<>();          return responseentity.ok("c รจ figa");     }      @requestmapping(value = "getbestlisthotel",             method = requestmethod.post,             consumes = mediatype.application_json_value,             produces = mediatype.application_json_value)     public responseentity<list<accomodationdto>> getholtelistfromplace(@valid @requestbody mainsearchlisthotelvo searchobject) {         list<accomodationdto> accomodationdtos = new arraylist<>();          log.debug("search vo in input: " + searchobject);         try {             accomodationdtos = mainsearchservices.getbesthotellistfromplace(searchobject);         } catch (exception e) {             log.error("exception: ", e);             return responseentity.status(httpstatus.bad_request).body(null);         }          if (accomodationdtos.isempty()) {             return responseentity.status(httpstatus.no_content).body(null);         }          return responseentity.ok(accomodationdtos);     }      @requestmapping(value = "gethotelbyid",             method = requestmethod.get,             consumes = mediatype.application_json_value,             produces = mediatype.application_json_value)     public responseentity<accomodationdto> getholtelbyid( integer id ) {         accomodationdto accomodationdto = null;          try {             accomodationdto = mainsearchservices.gethotelfromid(id);         } catch (exception e) {             log.error("exception: ", e);             return responseentity.status(httpstatus.bad_request).body(null);         }          if ( accomodationdto==null ) {             return responseentity.status(httpstatus.no_content).body(null);         }          return responseentity.ok(accomodationdto);     }      @requestmapping(value = "getlistroombyhotelid",             method = requestmethod.get,             consumes = mediatype.application_json_value,             produces = mediatype.application_json_value)     public responseentity<list<roomdto>> getlistroombyhotelid( integer hotelid ) {         list<roomdto> roomdtos = null;          try {             roomdtos = mainsearchservices.getlistroombyhotelid(hotelid);         } catch (exception e) {             log.error("exception: ", e);             return responseentity.status(httpstatus.bad_request).body(null);         }          if ( roomdtos==null ) {             return responseentity.status(httpstatus.no_content).body(null);         }          return responseentity.ok(roomdtos);     }  } 

then have junit test class named placesearchercontrollertest containing code:

@runwith(springrunner.class) @springboottest(webenvironment= springboottest.webenvironment.random_port) public class placesearchercontrollertest {      @autowired     private placesearchercontroller placesearchercontroller;      @test     public void placesearchercontrollertest() {         system.out.println("placesearchercontrollertest start");         system.out.println("placesearchercontrollertest end");     }  } 

at time placesearchercontrollertest() method nothing, trying run junit test method defined junit class obtain placesearchercontroller controller instance.

so problem running previous placesearchercontrollertest() test method obtain following error message:

java.lang.illegalstateexception: unable find @springbootconfiguration, need use @contextconfiguration or @springboottest(classes=...) test      @ org.springframework.util.assert.state(assert.java:392)     @ org.springframework.boot.test.context.springboottestcontextbootstrapper.getorfindconfigurationclasses(springboottestcontextbootstrapper.java:173)     @ org.springframework.boot.test.context.springboottestcontextbootstrapper.processmergedcontextconfiguration(springboottestcontextbootstrapper.java:133)     @ org.springframework.test.context.support.abstracttestcontextbootstrapper.buildmergedcontextconfiguration(abstracttestcontextbootstrapper.java:409)     @ org.springframework.test.context.support.abstracttestcontextbootstrapper.builddefaultmergedcontextconfiguration(abstracttestcontextbootstrapper.java:323)     @ org.springframework.test.context.support.abstracttestcontextbootstrapper.buildmergedcontextconfiguration(abstracttestcontextbootstrapper.java:277)     @ org.springframework.test.context.support.abstracttestcontextbootstrapper.buildtestcontext(abstracttestcontextbootstrapper.java:112)     @ org.springframework.boot.test.context.springboottestcontextbootstrapper.buildtestcontext(springboottestcontextbootstrapper.java:78)     @ org.springframework.test.context.testcontextmanager.<init>(testcontextmanager.java:120)     @ org.springframework.test.context.testcontextmanager.<init>(testcontextmanager.java:105)     @ org.springframework.test.context.junit4.springjunit4classrunner.createtestcontextmanager(springjunit4classrunner.java:152)     @ org.springframework.test.context.junit4.springjunit4classrunner.<init>(springjunit4classrunner.java:143)     @ org.springframework.test.context.junit4.springrunner.<init>(springrunner.java:49)     @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method)     @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62)     @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45)     @ java.lang.reflect.constructor.newinstance(constructor.java:422)     @ org.junit.internal.builders.annotatedbuilder.buildrunner(annotatedbuilder.java:104)     @ org.junit.internal.builders.annotatedbuilder.runnerforclass(annotatedbuilder.java:86)     @ org.junit.runners.model.runnerbuilder.saferunnerforclass(runnerbuilder.java:59)     @ org.junit.internal.builders.alldefaultpossibilitiesbuilder.runnerforclass(alldefaultpossibilitiesbuilder.java:26)     @ org.junit.runners.model.runnerbuilder.saferunnerforclass(runnerbuilder.java:59)     @ org.junit.internal.requests.classrequest.getrunner(classrequest.java:33)     @ org.junit.internal.requests.filterrequest.getrunner(filterrequest.java:36)     @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:96)     @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:42)     @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:262)     @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:84)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:497)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:147)   process finished exit code -1 

why? wrong? missing? how can solve issue , correctly obtain placesearchercontroller instance junit test class?

the error message me that: unable find @springbootconfiguration, need use @contextconfiguration or @springboottest(classes=...) test

what these @springbootconfiguration, @contextconfiguration , @springboottest?

reading here: http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/springbootconfiguration.html seems me @springbootconfiguration can used provide beans definitions (as @configuration class in spring not using spring boot).

my test class annotate @springboottest(webenvironment= springboottest.webenvironment.random_port) , reading here: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html that:

spring boot provides @springboottest annotation can used alternative standard spring-test @contextconfiguration annotation when need spring boot features. annotation works creating applicationcontext used in tests via springapplication

so think have application context take controller instance. have no idea how solve it. think missing , solution have pretty simple because have controller class , test class. can me showing me how can solve?

tnx

let's assume controller located in com.example.foo.controller , test in same directory. let's assume main class (your @springbootapplication) located in com.example.foo.app. you've tuned component scan directive since have unusual setup.

this has impacts on tests too! testing support attempt locate @springbootconfiguration (@springbootapplication @springbootconfiguration) looking in parent package(s) find match. if not throw exception.

the key this section of doc

the search algorithm works package contains test until finds @springbootapplication or @springbootconfiguration annotated class. long you’ve structured code in sensible way main configuration found.

so searches such match in com.example.foo.controller, com.example.foo com.example com. , gives , throw exception.

you haven't given context in @springboottest doeesn't know context start. try add reference @springbootapplication in there, like:

@springboottest(classes = myapp.class, webenvironment= springboottest.webenvironment.random_port) 

if assumptions correct, highly recommend review package structure.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -