junit - Static field injection in Spring Unit test -


i using junit4 in application tried test userservice, current test case simple:

create user named 'admin' , save database.  other test case rely on user. 

so use beforeclass insert record, have use userservice save user, spring not support inject static fields.

when tried create userservice manually, found have fill dependencies myself, wonder if there alternative? or there wrong how use junit?

@runwith(springjunit4classrunner.class) @contextconfiguration(locations = {"classpath:spring/application-config.xml"}) public class userservicetest {     @rule     public final expectedexception exception = expectedexception.none();      @autowired     private userservice userservice;      //@autowired  // sprint not suport     private static userservice us;     private static user u;      @beforeclass     public static void before() {         = new userserviceimpl(); // userservice instance can not used, since have fill dependencies manually          us.clear();         u = new user();         u.setusername("admin");         u.setpassword("pass");         us.save(u);      }      @afterclass     public static void after() {         userservice.delete(u.getid());     }       @test     public void testquery() {         list<user> list = userservice.find();         assertequals(1, list.size());     }      @test     public void testchangepassword() {         userservice.changepassword(u.getid(), u.getpassword(), "newpass");         assertequals("newpass", userservice.findone(u.getid()).getpassword());           exception.expect(resourcenotfoundexception.class);         userservice.changepassword("1", "32", "ss");          exception.expect(resourcenotfoundexception.class);         userservice.changepassword(u.getid(), "32", "ss");     } } 


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -