无法在unitTests中加载ApplicationContext无xml spring

无法在unitTests中加载ApplicationContext无xml spring,spring,unit-testing,Spring,Unit Testing,我刚刚创建了一个没有XMLSpring的简单mvc应用程序,在源代码中spring可以很好地与搜索bean和依赖注入配合使用。但我对单元测试有问题 我的主包是org.mycompany.app,我有相应的测试包 在测试包中,我有一个配置类: import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Confi

我刚刚创建了一个没有XMLSpring的简单mvc应用程序,在源代码中spring可以很好地与搜索bean和依赖注入配合使用。但我对单元测试有问题

我的主包是org.mycompany.app,我有相应的测试包

在测试包中,我有一个配置类:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan({ "org.mycompany.app" })
public class ConfigTest {

    public ConfigTest() {
        super();
    }
}
和org.mycompany.core.domain包中的简单测试

import org.mycompany.app.core.domain.events.SimpleEventPublisher;
import org.mycompany.test.ConfigTest;

import junit.framework.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 org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes = {ConfigTest.class})
public class BasicTestSample {

    @Autowired
    SimpleEventPublisher publisher;

    @Test
    public void testIfTestsWorks(){

        System.out.println("#### Test Runed:)");

        Assert.assertEquals(true, true);
    }
}
SimpleEventPublisher是包中核心应用程序中src包中的组件类 包org.mycompany.app.core.domain.events; 所以我认为它应该是自动扫描和自动连线的

但当我运行此测试时,出现以下异常: 我不知道是否需要使用更多的配置类或重用应用程序中的配置

        web - 2014-06-02 18:17:15,170 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener      [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@550ee7e5] to prepare test instance [org.finance.app.BasicTestSample@5f9b2141]
    java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
        at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) [junit-dep-4.10.jar:na]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) [junit-dep-4.10.jar:na]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) [junit-dep-4.10.jar:na]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) [junit-dep-4.10.jar:na]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) [junit-dep-4.10.jar:na]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) [junit-dep-4.10.jar:na]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) [junit-dep-4.10.jar:na]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176) [spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157) [junit-dep-4.10.jar:na]
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) [junit-rt.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_05]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_05]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_05]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_05]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) [idea_rt.jar:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) ~[spring-context-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) ~[spring-test-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    ... 28 common frames omitted
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
已解决: 问题是,我忘记了使用mvc应用程序中的bean的测试需要@WebAppConfiguration注释

我将注释更改为:

 @RunWith(SpringJUnit4ClassRunner.class)
 @WebAppConfiguration
 @ContextConfiguration(
    classes = ConfigTest.class,
    loader = AnnotationConfigWebContextLoader.class)
 public class BasicTestSample {
这对我很有用,谢谢

我在评论中建议使用@M.Deinum said

这对我帮助很大。谢谢@M.Denium

但我还是想知道我们能不能集中这2个注释 因为我在使用annotation@WebAppConfiguration

在控制器测试中也是如此

我可以集中注释吗


你看过堆栈跟踪了吗<代码>需要ServletContext来配置默认servlet处理。您正在尝试在非web相关应用程序上下文中加载web相关bean。那是行不通的。你需要用
@WebAppConfiguration
注释你的应用程序,你可以从
@ContextConfiguration
注释中省略
加载程序。好的,所以我在ConfigTest类中添加了@WebAppConfiguration注释,并从测试中删除加载程序。好的,错误在于web测试需要注释@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
 @WebAppConfiguration
 @ContextConfiguration(
    classes = ConfigTest.class)
 public class BasicTestSample {}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringProfileWithHkAndYamlApplication.class)
@WebAppConfiguration
@ComponentScan(basePackages = { "com.sample" })
public class SpringProfileWithHkAndYamlApplicationTests {}
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SpringProfileWithHkAndYamlApplicationTests.class})
public class WelcomeControllerTests {