基于SpringJUnit4ClassRunner类未找到(SpringJUnit4ClassRun

2019-07-05 16:33发布

我试图创建一个使用基于SpringJUnit4ClassRunner单元测试,但每次我执行它说用MVN安装它无法找到基于SpringJUnit4ClassRunner测试。

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

这里是我的代码

@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);
    }

这里是我的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>

我使用SpringSource工具套装IDE开发今年春天MVC应用程序,并在IDE它强调基于SpringJUnit4ClassRunner作为一个错误说它不能任凭我来解决,包括在我的POM文件中的所有必要的依赖。

谢谢

Answer 1:

我下载了你的代码,并得到了它,因为我包含在相关的导入语句在本地工作,只要:

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

也许你缺少的地方进口。



Answer 2:

我固定的这一个

  • 删除我的本地Maven回购(〜/ .m2目录/库)和
  • 在Eclipse迫使一个新的构建(项目 - >清洁...)

从AppFuse的论坛来源



Answer 3:

仅供参考,在你的pom.xml添加这个

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


Answer 4:

如果您使用的是Linux,打开termial携手下面的命令。 不要忘了添加用户的地方,“用户名”是

CD /home/you-user/.m2/repository/须藤RM -r的JUnit



Answer 5:

可能是你失去了包

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


文章来源: SpringJUnit4ClassRunner class not found