单元测试action -1

1 环境搭建:

First, include theplugin your POM (if using Maven 2) under the test scope:

<dependency>

  <groupId>org.apache.struts</groupId>

  <artifactId>struts2-junit-plugin</artifactId>

  <version>STRUTS_2_VERSION</version>

  <scope>test</scope>

</dependency>

if you are usingSpring, also add:

<dependency>

    <groupId>org.apache.struts</groupId>

    <artifactId>struts2-spring-plugin</artifactId>

    <version>STRUTS_2_VERSION</version>

</dependency>

<dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>jsp-api</artifactId>

    <version>2.0</version>

    <scope>test</scope>

</dependency>

 

 

在pom.xml中去掉 零配置插件

<groupId>org.apache.struts</groupId>

           <artifactId>struts2-convention-plugin</artifactId>

           <version>${struts.version}</version>

      

添加

<dependency>

         <groupId>com.sun</groupId>

         <artifactId>tools</artifactId>

         <version>1.5.0</version>

         <scope>system</scope>

         <systemPath>${java.home}/lib/tools.jar</systemPath>

     </dependency>

<java.home>D:\ProgramFiles\Java\jdk1.6.0_10</java.home>

启动的时候会从javajdk安装目录下的javaw.exe否则启动会从window32 下的javaw.exe

 

注意:Spring中对action的作用域需要为prototype 否则会报不能实例化action,需要每个测试的方法都是相互独立的

<bean id="BrUnitRuleAction"class="com.ultratest.businessTest.action.businessRequest.BrUnitRuleAction"scope="prototype">

</bean>

 

2 测试普通的Action(有返回值的action)

       ActionProxy proxy = getActionProxy("/Login.action");

       LoginActionaction =(LoginAction)proxy.getAction();

       String s = action.execute();

       assertEquals("success", s);

3 测试ajax的action

String s = executeAction("/Login.action");   //测试ajax的类,sresponse里的输出数据,若没有返回值为空字符串

assertEquals("{success:true}", s);

如果response里没有返回值:assertEquals(“”,s);

4 failure和error的区别

写@test时一般不捕获异常而抛出异常

可以观察junit的errors 如果有异常会提示(error是测试方法异常,被测试的方法如果抛异常会通过action.execute()或executeAction(“/Login.action”)抛出ServletException)

只有保证没有errors的情况下才能保证failures的测试正确性(failures是测试失败)

5 解决hibernate中lazy问题:

private SessiongetSession(SessionFactory sessionFactory) throwsDataAccessResourceFailureException {

       Sessionsession = SessionFactoryUtils.getSession(sessionFactory, true);

       FlushModeflushMode = FlushMode.NEVER;

 

       if (flushMode != null) {

           session.setFlushMode(flushMode);

       }

       return session;

    }

 

    private SessionFactorylookupSessionFactory(HttpServletRequest request) throws Exception {

 

       return (SessionFactory) this.applicationContext.getBean("sessionFactory");

    }

 

    @Override

    protected void setUp() throws Exception {

       super.setUp();

       SessionFactorysessionFactory = lookupSessionFactory(request);

       SessionhibernateSession = getSession(sessionFactory);

       TransactionSynchronizationManager.bindResource(sessionFactory,new SessionHolder(hibernateSession));

       //已登录

       User u = new User();

       u.setName("admin");

       u.setPassword("admin");

       request.getSession().putValue(Constants.SESSION_USER_KEY, u);

    }

这样关于lazy加载的问题解决。其实这个时限还是参考了Spring中OpenSessionInViewFilter的。

 

   首先,通过lookupSessionFactory()函数来获取这个测试环境中的org.hibernate.SessionFactory实体对象,其中的applicationContext是org.springframework.context.ApplicationContext的实现org.springframework.context.support.GenericApplicationContext,我们可以通过它的getBean方法来获取你配置文件中配置的SessionFactory。使用注解是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,不需要使用注解的时候可以用org.springframework.orm.hibernate.LocalSessionFactoryBean来实现。

   然后,在通过getSession()来获取其中的一个session,获取session的过程类似于java jdk 1.4以后的ThreadLocal类的实现方法,为每一个线程(用户的每一个request请求)提供一个“私有的”session实体类。关于ThreadLocal是干什么的,这里就不多讲了。所以我们在获取Session的时候,将这个session的FlushMode设置为FlushMode.NEVER,这样就可以让Spring来管理这个session,使得hibernate查询结束后,不会关闭session,从而解决lazy加载的问题。

   最后,调用Spring提供的TransactionSynchronizationManager.bindResource()方法将session绑定到该请求的resources中,值得一提的是,其中的resources也是通过ThreadLocal来实现跟线程绑定的。

关于实现web session的问题,很简单,该类提供了一个MockHttpServletRequest成员变量,我们只要mock一个session出来,然后加入到这个request中,就可以实现session的模拟了。示例代码如下:

6 junit注解失效

继承:StrutsSpringTestCase后junit的注解@Before和@after不起作用了

采用了StrutsTestCase 的setUp()和tearDown()方法代替

其他注解也不能用了

BeforeClass和AfterClass注解也不能用(必须是静态的)

7 命名规则:

类名:ClassNameTest

方法名:testMethodName

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值