java - Spring junit NoSuchMethodException

标签 java spring junit

我的 junit 测试用例出现以下错误。谁能建议我应该做什么来清除这个错误。我正在使用带有 spring 4.3.2 和 junit 4.12 的 Maven 构建工具。

org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
07-Sep-2016 18:19:17 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@691f36, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@18020cc, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@e94e92, org.springframework.test.context.support.DirtiesContextTestExecutionListener@12558d6, org.springframework.test.context.transaction.TransactionalTestExecutionListener@eb7859, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@12a54f9]
07-Sep-2016 18:19:18 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [testContext.xml]
07-Sep-2016 18:19:18 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@df8f5e: startup date [Wed Sep 07 18:19:18 IST 2016]; root of context hierarchy
07-Sep-2016 18:19:18 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [ldap_DEV.properties]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
07-Sep-2016 18:19:18 org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@e94e92] to prepare test instance [com.bt.dnp.ldap.util.LdapSyncTest@9505f]
java.lang.NoSuchMethodError: org.springframework.aop.scope.ScopedProxyUtils.isScopedTarget(Ljava/lang/String;)Z
    at org.springframework.context.event.EventListenerMethodProcessor.afterSingletonsInstantiated(EventListenerMethodProcessor.java:79)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:796)
    at

我的java类:我正在使用java 1.8。

import java.io.UnsupportedEncodingException;
import javax.naming.NamingException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:testContext.xml")
public class LdapSyncTest {
    private LdapSync ldapSync = null;

    @Autowired
    private LdapTemplate ldapTemplate;

    @Before
    public void init() {
        ldapSync = new LdapSync();
        ldapSync.setLdapTemplate(ldapTemplate);
    }

    @Test
    public void testPasswordSync() throws UnsupportedEncodingException, NamingException {
        ldapSync.doPwdUpdate("rajak1@bt.com", "uid", "BTCOM", "Tcs@12345");
    }
}

最佳答案

尝试将其添加到您的 pom 中:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.3.2.RELEASE</version>
</dependency>

您的错误表明spring-context正在尝试调用spring-aopScopedProxyUtils.isScopedTarget(String beanName)

该方法存在于 4.3.2 中(自2.5起),这只能说明spring-aop的运行时依赖是错误的。

在添加上面的依赖之前,你可以进入控制台并输入type mvn dependency:tree -Dincludes=org.springframework:spring-aop,你会发现 错误版本来自何处。

关于java - Spring junit NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39370439/

相关文章:

java - 如何在Android的mapbox sdk中绘制弯曲的折线

java - 如何在 Selenium WebDriver 中使用 @FindBy 注解

eclipse - grizzly 记录到 stderr,在 eclipse 中很烦人

java - 如何使用 Powermock、easymock、mockito 进行单元测试 DriverManager.getConnection ("") 或 DriverManager.getConnection ("","","") 调用

java - Spring 的 ApplicationContext 的静态单例实例

java - 如何通过关闭JFrame直接关闭JOptionPane?

java - Spring 3.0 无法加载属性

java - 使用 JUnit 测试 void 方法

java - 带 Spring 的 Hazelcast 配置

javascript - AngularJs 的错误 block 没有被执行