SSH框架下單元測試的實現


SSH框架下單元測試的實現

實現的功能

  • 實現了部門的增刪改查
  • 對Action進行了單元測試
  • 對Service 進行了單元測試,通過mock的方式實現。

實現的步驟

一、對Action層的單元測試實現
1、首先在pom文件中需要引入的依賴

    
     <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-junit-plugin</artifactId>
        <version>2.1.8</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.2.8.RELEASE</version>
      </dependency> 
            

說明:
在此處我們引入struts2-junit-plugin.2.1.8.jar,因為其里面有測試Struts2中action的類
StrutsSpringTestCase,用來測試ssh中的action。
2、新建一個測試類
如果你使用的是idea,那么你可以直接在需要建立測試類的Action類中
按ctrl+shift+t,新建一個測試類。如DepartmentActionTest
3、如果是普通的action的話,
待測試代碼:

       public String findById() {
              String did = ServletActionContext.getRequest().getParameter("did");
              department=departmentService.findByDid(Integer.valueOf(did));
              return "goEditDepartment";
          }  
    

測試代碼:

       @Test
          public void testFindById() throws Exception {
               /*這個函數相當@Before注解的函數,是調用單元測試后的時候,
             首先會執行的方法。可以在這里面做一些必要的准備工作*/
              request.setParameter("did", "1");
              ActionProxy proxy = getActionProxy("/department_findById.action");
              DepartmentAction action = (DepartmentAction) proxy.getAction();
              String result = action.findById();
              Assert.assertEquals("goEditDepartment", result);
          }

4、如果是返回json的action的話,待測試代碼:

 public void  findAll() {
         List<Department> departmentList= departmentService.findAll();
         JSONObject jsonObject = new JSONObject();
         if (CollectionUtils.isEmpty(departmentList)) {
             jsonObject.put("key", "success");
             jsonObject.put("data", JSON.toJSONString(departmentList));
             writeJson(jsonObject);
             return;
         }
         jsonObject.put("key", "success");
         jsonObject.put("data", JSON.toJSONString(departmentList));
         writeJson(jsonObject);
     }

測試代碼:

String result = executeAction("/json_findAll.action");
        Assert.assertNotNull(result);
        JSONObject jsonObject = JSON.parseObject(result);
        if ("success".equals(jsonObject.getString("key"))) {
            List<Department> departmentList = JSON.parseArray(jsonObject.getString("data"), Department.class);
            if (CollectionUtils.isEmpty(departmentList)) {
                return;
            }
            for (Department department : departmentList) {
                System.out.println(department.toString());
            }
        }

5、關於lazy問題的解決:首先在setUp()函數中加入下述代碼:

     @Override
         protected void setUp() throws Exception {
             super.setUp();
             SessionFactory sessionFactory = lookupSessionFactory(request);
             Session hibernateSession= getSession(sessionFactory);
             TransactionSynchronizationManager.bindResource(sessionFactory,
                     new SessionHolder(hibernateSession));
             //在只讀模式下(FlushMode.NEVER/MANUAL)寫操作不被允許
             hibernateSession.setFlushMode(FlushMode.AUTO);
         }
         

然后在添加兩個私有函數

 private Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
        Session session = sessionFactory.openSession();
        FlushMode flushMode = FlushMode.NEVER;
        if (flushMode != null) {
            session.setFlushMode(flushMode);
        }
        return session;
    }
    private SessionFactory lookupSessionFactory(HttpServletRequest request) {
        //“sessionFactory”是你spring配置文件(通常是application.xml)中的SessionFactory。
        //如:org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean
        return (SessionFactory)this.applicationContext.getBean("sessionFactory");
    }
 

說明:通過lookupSessionFactory()方法獲取這個測試環境中的org.hibernate.SessionFactory實體對象,其中
applicationContext是org.springframework.context.ApplicationContext的實現org.springframework.context.support.GenericApplicationContext
我們可以通過它的getBean方法來獲取你配置文件中配置的SessionFactory。使用注解是org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean
不需要使用注解的時候可以使用org.springframework.orm.hibernate.LocalSessionFactoryBean來實現。

該單元測試需要加載spring配置文件信息,默認加載路徑是你項目的src目錄下,文件名默認為applicationContext.xml,如果路徑不對或者
文件名不同,則需要重寫getContextLocations()方法,如

 protected String getContextLocations() {
        return "classpath*:applicationContext.xml";
    }

關於實現web session的問題,很簡單,該類提供了一個MockHttpServletRequest成員變量,我們只要mock一個session出來,然后加入到這個request中,就可以實現session的模擬了。示例代碼如下:

  HttpSession  session = new MockHttpSession();
 String sessionId = UUID.randomUUID().toString();
 session.setAttribute(ConstParameter.USER_SESSION, sessionId);
 //user是一個用戶信息的類,你可以根據你的需要自己定義
 UserInfor user = new UserInfo();
 user.setUserId(1);
 user.setName("xxx");
 session.setAttribute(ConstParameter.USER_INFO, user);
 request.setSession(session);
 

關於action的單元測試我們就說完了。
二、Service的單元測試
接下來我們在說說關於service的測試,
同樣,我們先從依賴說起,需要添加的依賴:

    <powermock.version>1.7.1</powermock.version>

   <!--********************powermock使用*********************-->
        <dependency>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-module-junit4</artifactId>
          <version>${powermock.version}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-api-mockito</artifactId>
          <version>${powermock.version}</version>
          <scope>test</scope>
        </dependency>
  

此處我們采用的是powermock+junit 進行mock測試。為啥使用powermock呢,毋庸置疑,由於他的功能比較強大
1、首先,我們我們看看待測試的代碼:

 @Override
    public List<MyInvoice> getMyInvoice(String buyerId) {
        if (StringUtils.isBlank(buyerId)) {
            return null;
        }
        List<MyInvoice> myInvoices = new ArrayList<MyInvoice>();
        String url = baseDataUrl + UrlConfig.GET_MYINVOICE_URL + "?t=" + VerifyBaseUtil.getT()
                + "&token=" + VerifyBaseUtil.getToken() + "&buyerId=" + buyerId;
        System.out.println("MyInvoiceServiceImpl getMyInvoice接口請求參數為:" + url);
        try {
            String responseInfo = HttpUtil.getHttp(url);
            System.out.println("MyInvoiceServiceImpl getMyInvoice接口返回結果為:" + responseInfo);
            Map<String, Object> result = JSON.parseObject(responseInfo, Map.class);
            if (DistrictReturnNum.SUCCESS.getValue().equals(result.get("code"))) {
                myInvoices = JSON.parseArray(JSON.toJSONString(result.get("result")), MyInvoice.class);
                return myInvoices;
            }
        } catch (Exception e) {
            System.out.println("MyInvoiceServiceImpl getMyInvoice 程序出錯,查詢發票失敗"+e.getMessage());
            return null;
        }
        return null;
    }

getMyInvoice方法是一個調用外部接口的方法,通過http協議進行通信。這兒有兩個問題
1.HttpUtil.getHttp(url) 是一個靜態方法,我們如何mock?
2.我們如何mock這個方法所在的實現類?因為該實現類是通過spring ioc 容器生成並注入的。

要回答這兩個問題,我們首先需要看看,我們的測試代碼:

@RunWith(PowerMockRunner.class)
@PrepareForTest(HttpUtil.class)
public class MyInvoiceServiceImplTest {
    @InjectMocks
    private MyInvoiceService myInvoiceService = new MyInvoiceServiceImpl();
    @Before
    public void setUp(){
        PowerMockito.mockStatic(HttpUtil.class);
    }
    @Test
    public void testGetMyInvoice() throws Exception {
        String result_http="{\"result\":[{\"addDate\":1509010776000,\"buyerId\":" +
                "\"9E59A2D27B7748848FB65041B854240E\",\"headName\":\"項偉測試\"," +
                "\"headType\":\"0\",\"invoiceId\":\"9747A51B57FF4EA781F1CFDF73A0D9DF\"," +
                "\"invoiceType\":\"0\",\"isDefault\":0},{\"addDate\":1509092635000,\"" +
                "buyerId\":\"9E59A2D27B7748848FB65041B854240E\",\"editDate\":1509094177000,\"headName\":\"項偉測試二\",\"headType\":\"0\",\"invoiceId\":\"720CF6C50E594283B01C79D03D6D52B2\"" +
                ",\"invoiceType\":\"0\",\"isDefault\":1}],\"msg\":\"成功\",\"code\":104}";
//      1、  buyerId為空
        String buyerId = null;
        Assert.assertEquals(null, myInvoiceService.getMyInvoice(buyerId));
//        2、buyerId不為空
        buyerId = "FF8080810F5E601526";
        PowerMockito.when(HttpUtil.getHttp(anyString())).thenReturn(result_http);
        List<MyInvoice> result = myInvoiceService.getMyInvoice(buyerId);
        Assert.assertEquals(2,result.size());
    }
  }
  

第一個問題:我們通過PowerMockito.mockStatic(HttpUtil.class); 一個靜態方法的實現類。然后就可以調用該靜態方法。
第二個問題:@InjectMocks注解來mock我們需要測試的業務類。
至此,我們就可以通過powermock對service層的方法進行單元測試了。

運行環境

  • 環境描述:Struts2+Spring4.2.4+hibernate4
    JAVA 1.7
  • 額外依賴第三方jar包,請參考pom.xml


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM