測(cè)試ssh框架的項(xiàng)目,需要加載spring配置文件applicationContext.xml,數(shù)據(jù)庫(kù)連接的文件hibernate.xml。此項(xiàng)目中的hibernate文件整合到了spring配置文件中。
上篇日志<junit>提到BeforeClass是“所有測(cè)試方法執(zhí)行之前首先執(zhí)行此方法,可用于數(shù)據(jù)庫(kù)連接,加載配置文件等” ,所以將加載spring配置文件的代碼寫在這里,但是BeforeClass下寫的函數(shù)得是靜態(tài)的
public class userservicetest {
public static UsersService usersService;
@BeforeClass
public staticvoid onSetUpBeforeTransaction() throws Exception {
ApplicationContext context = new FileSystemXmlApplicationContext(
new String[ ] {
"/WebRoot/WEB-INF/applicationContext.xml"
}
);
usersService = (UsersService) context.getBean("usersService");
}
@Test
public void testfindUserByAccountNames() {
String userId="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
try {
String realname= usersService.findUserByAccountNames(userId);
assertThat(realname, is("管理員"));
Assert.assertEquals("管理員", realname);
} catch (CRUDException e) {
e.printStackTrace();
}
}
}