java - @ComponentScan not working in test with spring-boot-starter-test -


i attempting test @service , @repository classes in project spring-boot-starter-test , @autowired not working classes i'm testing.

unit test:

@runwith(springjunit4classrunner.class) //@contextconfiguration(classes = helloworldconfiguration.class, initializers = configfileapplicationcontextinitializer.class) @springapplicationconfiguration(classes = helloworldrs.class) //@componentscan(basepackages = {"com.me.sbworkshop", "com.me.sbworkshop.service"}) //@configurationproperties("helloworld") @enableautoconfiguration //@activeprofiles("test") // class in src/test/java/ , builds target/test-classes public class helloworldtest {      @autowired     helloworldmessageservice helloworldmessageservice;      public static final string expected = "je pense donc je suis-testing123";      @test     public void testgetmessage() {         string result = helloworldmessageservice.getmessage();         assert.assertequals(expected, result);     } } 

service:

@service @configurationproperties("helloworld") // class in /src/main/java , builds target/classes public class helloworldmessageservice {      private string message;      public string getmessage() {         return message;     }      public void setmessage(string  message) {         this.message=message;     }  } 

the commented class annotations on unit test represent various things i've tried working. test , project packages in same package paths , @componentscan works fine entry point (@restcontroller class main method). service @componentscan's , @autowire's fine in @restcontroller class in src/main/java side, not in test. required add again @bean in @configuration class in order @autowired work. class otherwise in scope fine , can reference , instantiate fine test. problem appears @componentscan not appear correctly traverse multiple entries in test runner classpath, in case /target/test-classes , /target/classes.

the ide using intellij idea 13.

update - here helloworldrs , config:

@restcontroller @enableautoconfiguration @componentscan public class helloworldrs {      //    spring boot entry point - main() method     public static void main(string[] args) {         springapplication.run(helloworldrs.class);     }      @autowired     helloworldmessageservice helloworldmessageservice;      @requestmapping("/helloworld")     public string helloworld() {         return helloworldmessageservice.getmessage();     }  } 

...

@configuration public class helloworldconfiguration {      @bean     public map<string, string> map() {         return new hashmap<>();     }      // bean manually added workaround @componentscan problem     @bean     public helloworldmessageservice helloworldmessageservice() {         return new helloworldmessageservice();     }      // bean manually added workaround @componentscan problem     @bean     public helloworldrs helloworldrs() {         return new helloworldrs();     } } 

i don't know if turn out solution, don't use default package (i.e. don't put *.java in "src/main/java" directly), , don't use @componentscan or @enableautoconfiguration in default package. end killing application on startup tries scan on classpath (including spring libraries).


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -