简体   繁体   English

找不到 SpringJUnit4ClassRunner 类

[英]SpringJUnit4ClassRunner class not found

i am trying to create a unit test using SpringJUnit4ClassRunner but everytime i execute the test it says it cannot find SpringJUnit4ClassRunner using mvn install.我正在尝试使用 SpringJUnit4ClassRunner 创建一个单元测试,但是每次我执行测试时,它都说它无法使用 mvn install 找到 SpringJUnit4ClassRunner。

\src\test\java\com\jr\freedom\util\JsonParserUtilTest.java:11: cannot find symbol
[ERROR] symbol: class SpringJUnit4ClassRunner
[ERROR] -> [Help 1]

here is my code这是我的代码

@RunWith(SpringJUnit4ClassRunner.class)
public class JsonParserUtilTest {

    private String jsonUser = "{ \"username\":\"jono111\",\"emailAddress\":\"jon@google.com\", \"password\":\"12345678\",\"firstName\":\"jono\", \"surname\":\"richy\", \"country\":\"united kingdom\",\"bio\":\"Bio stuff goes here about the user. where he comes from etc etc. all is well. lets go go go\" }";

    @Test
    public void testToJsonObject() {

        User user = new User();
        user.setBio("bio mate");
        user.setCountry("uk");
        user.setEmailAddress("jonney@hello.com");
        user.setFirstName("jono");
        user.setPassword("passwordfdsadsa");
        user.setUsername("crazy8");
        JSONObject jsonUser =  JsonPojo.toJsonObject(user);
        assertNotNull(jsonUser);
        assertNotNull(jsonUser.keys());
        System.out.println(jsonUser);

    }

    @Test
    public void testToObject(){
        JSONObject jsonUser = JSONObject.fromObject(jsonUser);
    }

Here is my POM file:这是我的 POM 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>FreedomSpring</groupId>
    <artifactId>FreedomSpring</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <!-- specifiy which directory within the project hierarchy will be considered 
                        as the root directory from the generated war file -->
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- Dependencies below for spring, hibernate, json etc -->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.7.Final</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-mock</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
    </dependencies>


</project>

i am using springSource tool suit IDE to develop this spring mvc app and on the IDE it highlights SpringJUnit4ClassRunner as a error saying it cant be resolved despite me including all the necessary dependencies on my pom file.我正在使用 springSource 工具套件 IDE 来开发这个 spring mvc 应用程序,并且在 IDE 上它突出显示 SpringJUnit4ClassRunner 作为一个错误,说它无法解决,尽管我在我的 pom 文件中包含了所有必要的依赖项。

Thanks谢谢

I downloaded your code and got it working locally, as long as I included the relevant import statements:我下载了您的代码并使其在本地运行,只要我包含相关的导入语句:

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.junit.Test;
...

Perhaps you're missing an import somewhere.也许您在某处缺少导入。

FYI, Add this in your pom.xml仅供参考,将此添加到您的 pom.xml 中

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

I fixed this one by我修复了这个

  • Deleting my local maven repo (~/.m2/repository) and删除我的本地 maven repo (~/.m2/repository) 和
  • In eclipse forcing a new build (Project->Clean...)在 Eclipse 中强制进行新构建(Project->Clean...)

Sourced from the appfuse forums 来自 appfuse 论坛

If you are using linux, open the termial and work the command below.如果您使用的是 linux,请打开终端并执行以下命令。 Do not forget to add the user in the place where the "user-name" is不要忘记在“用户名”所在的地方添加用户

cd /home/you-user/.m2/repository/
sudo rm -r junit

I had to put spring-test jar at the "TOP" of Java Build Path我不得不将 spring-test jar 放在 Java Build Path 的“顶部” 构建路径图

Go to pom.xml and make sure your file contain this dependency:转到 pom.xml 并确保您的文件包含此依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>

Also make sure that in this dependency is no "scope" node:还要确保在此依赖项中没有“范围”节点:

<scope>test</scope>

Maybe you lost the package也许你丢了包裹

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>5.1.7.RELEASE</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM