Java 未找到默认构造函数;嵌套异常org.springframework.test.context.TestContext。<;初始化>;()

Java 未找到默认构造函数;嵌套异常org.springframework.test.context.TestContext。<;初始化>;(),java,spring,testing,Java,Spring,Testing,我写了这篇文章: 我试图为我的控制器编写一个测试: 我的代码: import static org.fest.assertions.Assertions.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.T

我写了这篇文章:

我试图为我的控制器编写一个测试:

我的代码:

import static org.fest.assertions.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, WebApplicationContext.class})
@WebAppConfiguration
public class SiteControllerTest {

    @Test
    public void shouldReturnCorrectSite() throws Exception {
        assertThat(true).isTrue();
    }



}
当我运行测试时,出现如下异常:

No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.test.context.TestContext.() at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)


java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)

...

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testContext': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.test.context.TestContext]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.test.context.TestContext.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)
未找到默认构造函数;嵌套的异常是java.lang.NoSuchMethodException:org.springframework.test.context.TestContext。()位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1007)
java.lang.IllegalStateException:未能加载ApplicationContext
位于org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
...
原因:org.springframework.beans.factory.BeanCreationException:创建名为“testContext”的bean时出错:bean实例化失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.test.context.TestContext]:未找到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:org.springframework.test.context.TestContext。()
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1007)

我做错了什么?

在原始文章中,您应该在类路径中定义TestContext类,例如:

@Configuration
public class TestContext {

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

        messageSource.setBasename("i18n/messages");
        messageSource.setUseCodeAsDefaultMessage(true);

        return messageSource;
    }

    @Bean
    public TodoService todoService() {
        return Mockito.mock(TodoService.class);
    }
}
其他替代方法可以直接使用xml文件而不是java类进行测试:

@ContextConfiguration(locations = {"classpath:testContext.xml", "classpath:exampleApplicationContext-web.xml"})

好的,您的TestContext类没有无参数构造函数,因为消息表明.TestContext.class是org.springframework.test.context中的一个类。我能做什么?它不适用于
ContextConfiguration
。我想他们在链接的教程中创建了自己的
TestContext
类。@user612925:对不起,我错过了。因为您将它放在配置类列表中,所以我认为它是您的自定义类。正如索蒂里奥斯所说,它不应该列在那里。