Search

Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Java - JUnit + WebSockets 無法進行測試

2015-09-24 8:03 PM

當專案內有設定 Websocket 相關的設定時

啟動JUnit時會發生 No suitable default RequestUpgradeStrategy found 錯誤

這是因為一般進行測試時並不會依賴於一個執行中的Tomcat Server

因此當 Websocket 設定讀取時 DefaultHandshakeHandler 會嘗試尋找 Server 的相關 class 導致錯誤

此時只要在測試階段加入以下嵌入式的 Tomcat Library 即可

程式碼範例
<!-- Libraries for testing Websocket -->
<dependency>
 <groupId>org.apache.tomcat.embed</groupId>
 <artifactId>tomcat-embed-core</artifactId>
 <version>8.0.8</version>
 <scope>test</scope>
</dependency>
<dependency>
 <groupId>org.apache.tomcat.embed</groupId>
 <artifactId>tomcat-embed-websocket</artifactId>
 <version>8.0.8</version>
 <scope>test</scope>
</dependency>
各項資料連結
Apache Tomcat
WebSockets

Java - 使用 Maven + JUnit 測試時中文出現亂碼

7:57 PM

若出現中文顯示為亂碼的情形

可直接在 pom.xml 加入編碼設定即可

程式碼範例
<!-- Surefire test plugin -->
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.17</version>
 <!-- 預防使用 Maven test 時中文顯示為亂碼 -->
 <configuration>
  <argLine>-Dfile.encoding=UTF-8</argLine>
 </configuration>
</plugin>
各項資料連結
Maven Surefire