Package testregister;
Import Java.util.HashSet;
Import Junit.framework.Assert;
Import Org.easybooks.bookstore.dao.IUserDAO;
Import Org.easybooks.bookstore.vo.User;
Import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
public class Testuserdao extends abstractdependencyinjectionspringcontexttests{
The set method of the private variable to inject the bean you want from the ApplicationContext, without the need for an explicit call to Applicationcontext.getbean (XXX)
This class will automatically inject you from the configuration file specified by the Getconfiglocations () method
Private Iuserdao Userdao;
public void Setuserdao (Iuserdao _userdao) {
Userdao=_userdao;
}
@Override
Protected string[] Getconfiglocations () {
Specify spring configuration file Applicationcontext.xml
return new string[] {"Classpath:org/easybooks/bookstore/test/applicationcontext.xml"};
}
Success:
/**
* Test the Register () method in Userdao--registration is successful
*/
public void testregistersuccess () {
User user = New User ("Wzw", "WZW", "Man", 22,new HashSet (0));
Boolean isexit = Userdao.exituser ("WZW");
Assert.assertfalse (Isexit);
Userdao.saveuser (user);
SYSTEM.OUT.PRINTLN ("Registered successfully, the user is:" +user.getusername ());
}
Failed:
/**
* Test the Register () method in Userdao--registration failed
*/
public void Testregisterfail () {
Boolean isexit = Userdao.exituser ("CCJ");
Assert.asserttrue (Isexit);
SYSTEM.OUT.PRINTLN ("registration failed");
}
}
Experiment Seven Web application test _ Write Unit test case, test the DAO layer of user registration function