有幾個問題
1)使用@RunWith(SpringJUnit4ClassRunner.class)
2)你的路徑聽起來像是你有一個Maven項目。在一個maven項目中,當測試運行時,src/test/resources
將被添加到類路徑中。因此,使用:
@ContextConfiguration(locations = "classpath:applicationContext.xml")
如果你的港灣,在src/main/resources
的其他文件枝條同一個名字的話,我推薦一個在src/test/resources
重命名爲test-applicationContext.xml
,然後用@ContextConfiguration(locations = "classpath:test-applicationContext.xml")
,只是爲了減少結的數量你腦
完整的示例:
package com.test.service;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classpath:test-ApplicationContext.xml)
public class WhateverTest {
...
}
POM摘錄:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
您通常通過首先閱讀異常堆棧跟蹤來解決問題。 –
對於初學者,您需要刪除@RunWith(Parameterized.class)並將其替換爲@RunWith(SpringJUnit4ClassRunner.class)(這意味着您的JUnit測試不會被參數化,並且您需要爲其找到其他方式參數化的功能) – geoand
用@RunWith(Parameterized.class)替換@RunWith(SpringJUnit4ClassRunner.class)..它顯示類錯誤消息類不能解析類型..如何reslove這個錯誤信息? –
user3411418