
- Spring WS 教程
- Spring WS - 主頁
- Spring WS - 概述
- Spring WS - 環境設定
- Spring WS - 第一個應用程式
- Spring WS - 靜態 WSDL
- Spring WS - 編寫伺服器
- Spring WS - 單元測試伺服器
- Spring WS - 編寫客戶端
- Spring WS - 單元測試客戶端
- Spring WS 實用資源
- Spring WS - 快速指南
- Spring WS - 實用資源
- Spring WS - 討論
Spring WS - 單元測試客戶端
在本章中,我們將學習如何利用 Spring WS 單元測試在 Spring WS - 編寫客戶端 中為在 Spring WS - 編寫伺服器 章節中建立的 Web 應用程式伺服器建立的客戶端進行單元測試。
步驟 | 說明 |
---|---|
1 | 按照 Spring WS - 編寫伺服器章節中的說明更新包 com.tutorialspoint 中的專案 countryService。 |
2 | 按照以下步驟建立包 com.tutorialspoint 下的 CountryServiceClientTest.java,位於資料夾 SRC → Test → Java 中。 |
CountryServiceClientTest.java
package com.tutorialspoint; import static org.junit.Assert.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import com.tutorialspoint.client.CountryServiceClient; public class CountryServiceClientTest { CountryServiceClient client; @Before public void setUp() throws Exception { client = new CountryServiceClient(); Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("com.tutorialspoint"); client.setMarshaller(marshaller); client.setUnmarshaller(marshaller); } @Test public void test() { GetCountryResponse response = client.getCountryDetails("United States"); Country expectedCountry = new Country(); expectedCountry.setCapital("Washington"); Country actualCountry = response.getCountry(); Assert.assertEquals(expectedCountry.getCapital(), actualCountry.getCapital()); } }
啟動 Web 服務
啟動 Tomcat 伺服器,確保我們能夠使用標準瀏覽器從 webapps 資料夾訪問其他網頁。
單元測試 Web 服務客戶端
開啟命令控制檯,轉到 C:\MVN\countryService 目錄並執行以下 mvn 命令。
C:\MVN\countryService>mvn test
Maven 將開始構建和測試專案。
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building countryService Spring-WS Application 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ countrySer vice --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 2 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ countryService --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b uild is platform dependent! [INFO] Compiling 10 source files to C:\MVN\countryService\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ co untryService --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\countryService\src\test\resour ces [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ country Service --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b uild is platform dependent! [INFO] Compiling 2 source files to C:\MVN\countryService\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ countryService --- [INFO] Surefire report directory: C:\MVN\countryService\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.tutorialspoint.CountryServiceClientTest Feb 27, 2017 8:45:26 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol Feb 27, 2017 8:45:26 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbC ontextFromContextPath INFO: Creating JAXBContext with context path [com.tutorialspoint] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.457 sec Running com.tutorialspoint.ws.CustomerEndPointTest Feb 27, 2017 8:45:27 PM org.springframework.test.context.TestContextManager retr ieveTestExecutionListeners INFO: @TestExecutionListeners is not present for class [class com.tutorialspoint .ws.CustomerEndPointTest]: using defaults. Feb 27, 2017 8:45:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionR eader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring-context.xml] Feb 27, 2017 8:45:27 PM org.springframework.context.support.GenericApplicationCo ntext prepareRefresh INFO: Refreshing org.springframework.context.support.GenericApplicationContext@5 17c642: startup date [Mon Feb 27 20:45:27 IST 2017]; root of context hierarchy Feb 27, 2017 8:45:28 PM org.springframework.ws.soap.addressing.server.Annotation ActionEndpointMapping afterPropertiesSet INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0] Feb 27, 2017 8:45:28 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.243 sec Feb 27, 2017 8:45:28 PM org.springframework.context.support.GenericApplicationCo ntext doClose INFO: Closing org.springframework.context.support.GenericApplicationContext@517c 642: startup date [Mon Feb 27 20:45:27 IST 2017]; root of context hierarchy Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.686 s [INFO] Finished at: 2017-02-27T20:45:28+05:30 [INFO] Final Memory: 17M/173M [INFO] ------------------------------------------------------------------------
廣告