junit测试ssh项目

上一篇 / 下一篇  2011-04-02 14:07:38 / 个人分类:junit单元测试

测试ssh框架的项目,需要加载spring配置文件applicationContext.xml,数据库连接的文件hibernate.xml。此项目中的hibernate文件整合到了spring配置文件中。

上篇日志<junit>提到BeforeClass是“所有测试方法执行之前首先执行此方法,可用于数据库连接,加载配置文件等” ,所以将加载spring配置文件的代码写在这里,但是BeforeClass下写的函数得是静态的

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();
        }
 }
}


TAG:

引用 删除 yamazakei   /   2011-04-11 16:31:09
5
 

评分:0

我来说两句

Open Toolbar