使用SpringFramework进行Junit测试,没有Maven:错误 - NoSuchMethodError

时间:2015-05-28 05:43:03

标签: java spring junit4

我是JUnit的新手 - Spring Framework。

我正在做的是为我的应用程序创建一些测试。这是Code我所做的。

@RunWith(SpringJUnit4ClassRunner.class)
public class InspirdTests {

    @Autowired
    private PlatformBLL platform;

    @Autowired
    private WebApplicationContext webAppContext;

    @Before
    public void settingUp(){
        System.out.println("Before SetUp");
    }

    @Test
    public void setUp() throws Exception{
        System.out.println("Before : " + platform);
        platform.getAllProjectsForPlatform(null);
        System.out.println("After : " + platform);
    }
}

和堆栈跟踪

java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:155)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我只是被封锁了,我坚持了下来,我不能继续前进。

我尝试过:

  1. 很多谷歌,但结果却是原样。
  2. 我做了一些空白的测试方法,但结果仍然是原样。
  3. 我已将JUnit-4.12.jar添加到 WEB-INF / lib /
  4. 我已将Junit 4添加到 Java构建路径
  5. 伙计们,请帮我找出解决方案。

1 个答案:

答案 0 :(得分:0)

在eclipse构建路径中包含spring-testjunit个jar。

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;


@ContextConfiguration(locations = { "classpath:/conf/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class MongoRepositoryImplTest {

    @Autowired
      MongoRepositoryImpl mongoRepositoryImpl;

    @Test
    public void testGetMongoConnectionFromFactory() {
        ...
    }
    ...

配置文件应如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

    <context:component-scan base-package="com.client.impl" />

    <bean id="mongoRepositoryImpl" class="com.client.impl.MongoRepositoryImpl">
        <constructor-arg name="connectionName" value="testCases"/>
    </bean>

</beans>