天天看點

Junit4在SSM中應用

項目背景

最近在看Spring相關知識,在看源碼之後,注意到項目結構是包含main和test2個檔案夾。main檔案包含的當然是源檔案,而test是針對src源檔案建構的測試類。具體如圖所示

Junit4在SSM中應用

單元測試:

web項目中怎麼針對某一子產品進行單元測試,我之前的方法是每次啟動tomcat,然後通過頁面點點去觸發需要測試的代碼。但這樣會帶來一個問題: 當項目小的時候,一切ok,沒問題。但是當項目大了之後(比如我現在做的這個項目),每次啟動都需要個10幾秒。這種單元測試方式每次測試都會花費大量的時間在tomcat啟動上,這其實不是我們想要的結果。

Junit4

JUnit是一個Java語言的單元測試架構,他可以幫助我們更加友善簡介的進行單元測試。

Junit4 在SSM中的應用

1 pom.xml引入jar包:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>4.3.7.RELEASE</version>
</dependency>
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
</dependency>
           

2 編寫測試用例:

偷學spring中格式,在和main同級的目錄下建立test檔案,并把test檔案 mark directory as—> test sources root.

//注解讓測試運作于Spring測試環境。
@RunWith(SpringJUnit4ClassRunner.class)
//引入Spring配置
@ContextConfiguration({"classpath*:/dispatcher-servlet.xml", "classpath*:/mybatis-config.xml"})
public class TestWeChat {

    @Test
    public void testWeChat() {
        List<String> strings = new ArrayList<>();
        strings.add("34920");
        strings.add("44568");
        WeChatMsgUtils.sendBroadCast(strings, "測試微信發送消息");
        WeChatMsgUtils.createSendGroupMsg(strings, "企業微信測試", "測試");
    }
}
           

但是這樣還不行,可能會報錯:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [WEB-INF/resources/]], resolvers=[[email protected]bf1dc]]] does not run in a WebApplicationContext but in: [email protected]18d1: startup date [Fri May  :: CST ]; root of context hierarchy
           

原因是:靜态資源的Handler的ResourceHttpRequestHandler不能建立。那麼我們可以通過指定靜态資源位置來建立。

也就是加上

@WebAppConfiguration("src/main/resources")
           

@WebAppConfiguration注解在類上,用來聲明加載的ApplicationContext是一個WebApplicationContext。它的屬性指定的是Web資源的位置,預設為src/main/webapp。

繼續運作 ,我擦,還是報錯:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSocketHandlerMapping' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketConfiguration.class]: 
           

這個報錯的原因是因為,我們在項目中用到了websocket。

google之後,發現是缺少一個引用:

<dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-websocket</artifactId>
     <version>7.0.52</version>
     <scope>test</scope>
 </dependency>
           

go on

終于沒有報錯了:結果如圖所示:

Junit4在SSM中應用

總結

在ssm中使用junit4 其實隻需要3步驟。

1 在pom.xml檔案中引用對應的包:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>4.3.7.RELEASE</version>
</dependency>
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
</dependency>
//項目中如果有用到websocket,需要添加下面的包,沒用到就算了
 <dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-websocket</artifactId>
     <version>7.0.52</version>
     <scope>test</scope>
 </dependency>
           

2 編寫對應的測試類:

//注解讓測試運作于Spring測試環境。
@RunWith(SpringJUnit4ClassRunner.class)
//引入Spring配置
@ContextConfiguration({"classpath*:/dispatcher-servlet.xml", "classpath*:/mybatis-config.xml"})
@WebAppConfiguration("src/main/resources")
           

3 需要測試方法加上@Test:

@Test
public void testWeChat() {
       List<String> strings = new ArrayList<>();
       strings.add("34920");
       strings.add("44568");
       WeChatMsgUtils.sendBroadCast(strings, "測試微信發送消息");
       WeChatMsgUtils.createSendGroupMsg(strings, "企業微信測試", "測試");
 }