2012-12-18 14 views
17

Sto provando a creare un unit test utilizzando SpringJUnit4ClassRunner ma ogni volta che eseguo il test dice che non riesce a trovare SpringJUnit4ClassRunner usando mvn install.Classe SpringJUnit4ClassRunner non trovata

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

Ecco il mio codice

@RunWith(SpringJUnit4ClassRunner.class) 
public class JsonParserUtilTest { 

    private String jsonUser = "{ \"username\":\"jono111\",\"emailAddress\":\"[email protected]\", \"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("[email protected]"); 
     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); 
    } 

Ecco il mio 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> 

sto usando SpringSource vestito strumento IDE per sviluppare questa applicazione MVC primavera e l'IDE mette in luce SpringJUnit4ClassRunner come un errore che dice che non può essere risolto nonostante io includa tutte le dipendenze necessarie sul mio file pom.

Grazie

+1

Si dovrebbe aggiungere portata di prova alle tue dipendenze di test, cioè 'junit',' spring-test', 'mockito-all', ecc ... Non penso che risolverà il tuo problema. – Jonathan

risposta

28

ho scaricato il codice e ottenuto che funziona localmente, fino a quando ho incluso le istruzioni di importazione rilevanti:

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

Forse vi state perdendo un'importazione da qualche parte.

+1

Questo ha fatto il trucco. ho sempre pensato che eclipse aggiungesse automaticamente le importazioni per me! – jonney

+1

Nessun problema, felice di poterti aiutare. – Jonathan

2

FYI, Aggiungere questo nella vostra pom.xml

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

Ho fissato questo uno per

  • Eliminazione mio repo locale Maven (~/.m2/repository) e
  • in Eclipse forzando una nuova build (Progetto-> Cancella ...)

Sourced from the appfuse forums

+0

Grazie! L'unica cosa che ha funzionato per me –

1

Se si utilizza linux, aprire il termial e utilizzare il comando seguente. Non dimenticare di aggiungere l'utente nel luogo dove il "nome utente" è

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