当前位置: 首页 > 工具软件 > Paoding-Rose > 使用案例 >

Paoding-Rose下的Junit测试

上官培
2023-12-01

Paoding-Rose下的Junit测试

新公司老项目都用的paoding-rose框架,想在里面写个单元测试来测试ServiceDao居然没有解决方案,参照正常的Spring Web项目的去配置test直接死翘翘.

花了点时间看了下Rose的源码.算是弄出来个解决方案.

不过话说9102年都快过完了,应该没哪个公司会在技术选型时再来选Rose了.目测搜到这篇文章的都是折翼的天使.

解决方案

maven工程为例,工程结构如下:

根目录
	--src
	  --main
		--java
		--resources   
	  --test
		--java
		--resources
  1. src/main/resources目录下的配置文件(可以只复制spring及spring中引用到的配置文件),复制到src/test/resources目录下.

  2. pom.xml中增加testResources配置

     <!-- 最好和自己的pom中的 resources 的配置保持一致 -->
     <testResources>
         <testResource>
             <directory>src/test/resources</directory>
             <includes>
                 <include>*</include>
                 <include>**/*</include>
             </includes>
         </testResource>
     </testResources>
    
  3. Spring的配置文件中,增如下配置:

     <!--  这里声明只是为了保证代码中有自动注入Invocation对象能正常注入 -->
     <bean id="mockHttpServletRequest" class="org.springframework.mock.web.MockHttpServletRequest"/>
     <bean id="mockHttpServletResponse" class="org.springframework.mock.web.MockHttpServletResponse"/>
     <bean id="requestPath" class="net.paoding.rose.web.RequestPath">
         <constructor-arg name="method" value="GET"/>
         <constructor-arg name="uri" value="/test"/>
         <constructor-arg name="ctxpath" value="ctx"/>
         <constructor-arg name="dispatcher" value="REQUEST"/>
     </bean>
     <bean id="invocationBean" class="net.paoding.rose.web.impl.thread.InvocationBean">
         <constructor-arg name="request" ref="mockHttpServletRequest"/>
         <constructor-arg name="response" ref="mockHttpServletResponse"/>
         <constructor-arg name="requestPath" ref="requestPath"/>
     </bean>
    
  4. src/test/java下,创建TestBase.java.具体代码如下:

     import net.paoding.rose.web.Dispatcher;
     import net.paoding.rose.web.Invocation;
     import net.paoding.rose.web.RequestPath;
     import net.paoding.rose.web.annotation.ReqMethod;
     import net.paoding.rose.web.impl.thread.InvocationBean;
     import org.junit.Before;
     import org.junit.Ignore;
     import org.junit.runner.RunWith;
     import org.springframework.data.domain.Pageable;
     import org.springframework.mock.web.MockHttpServletRequest;
     import org.springframework.mock.web.MockHttpServletResponse;
     import org.springframework.test.context.ContextConfiguration;
     import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     import org.springframework.transaction.annotation.Transactional;
     
     import javax.annotation.Resource;
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
     
     /**
     * 测试基类.
     * 所有的测试用例都必须继承这个类.
     * 默认所有test case执行完后都会回滚.
     *
     * @version 1.0
     * @date 2019-08-30 10:01
     */
     @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration(locations = {"classpath*:applicationContext*.xml"})
     @Transactional(value = "transactionManager")
     @Ignore
     public class TestBase {
     
     	@Resource
     	private Invocation inv;
     
     	@Before
     	public void beforeClass() {
     		// ...
     	}
     }
    

其它的测试用例直接继承这个类就行了.

 类似资料: