JUnit test with Spring and mongodb: could not autowire repository -
i have problem writing test spring , junit test mongodb respository. message "could not autowire. no beans of ... type found" repository is
public interface projectrepository extends mongorepository<project, string> { public project findbyname(string name); }
while configuration file is
@configuration @enablemongorepositories(basepackageclasses = repositorypackage.class) public class springmongoconfiguration extends abstractmongoconfiguration{ @override protected string getdatabasename() { return "test"; } @bean public mongoclient mongo() throws exception { mongoclient client = new mongoclient("localhost"); return client; } @bean public mongotemplate mongotemplate() throws exception { return new mongotemplate(mongo(), getdatabasename()); } }
where repositorypackage in same package of repository. test is
@runwith(springjunit4classrunner.class) @contextconfiguration(classes = springmongoconfiguration.class) public class mongodbprojecttests { @autowired private projectrepository projectrepository; @before public void init(){ projectrepository.deleteall(); project project = new project("sdfjklgh", 1, "comunione leonardo", project.type_project_photo, project.state_project_draft); projectrepository.save(project); } @test public void test1(){ list<project> list = projectrepository.findall(); assertequals(1, list.size()); } }
what doing wrong?
i've created project , copied files it, , works me.
the cause can guess projectrepository
class not in same package repositorypackage
class specify basepackageclasses
attribute component-scanning. (it should okay if projectrepository
in package inside package repositorypackage
in.)
i've published project github, uses gradle rather maven.
and another one, time using maven.
Comments
Post a Comment