使用Springboot @autowire进行测试不适用于@SpringBootTest

时间:2018-04-18 06:36:51

标签: java spring-boot junit spring-boot-test

我在springboot应用程序中编写Junits,它只有一个Initializer类

@SpringBootApplication 

public class Initializer extends SpringBootServletInitializer {

 @Override 
 protected SpringApplicationBuilder configure(SpringApplicationBuilder 
  application) {
      return application.sources(Initializer.class);
   }

 }

以及其他控制器和服务类。

我的Junit服务类看起来像:

@RunWith(SpringJUnit4ClassRunner.class)  
@SpringBootTest  
public class ServiceTest {

           @Autowired
            private CommonUtils commonUtils;

             -----do assert cases---------

  }

当我运行Junit时,它会抛出错误:

    08:20:27.481 [main] ERROR 
    org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1e67b872] to prepare test instance [<ServiceTest>@5d624da6]
    java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies (DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:276)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
... 24 common frames omitted 08:20:27.532 [main] DEBUG org.springframework.test.context.support.DirtiesContextTestExecutionListener - After test class: context [[TestContext@2ef9b8bc testClass = ServiceTest, locations = [null], testInstance = [null], testMethod = [null], testException = [null]]], dirtiesContext [false].

此外,我尝试过所有注释,例如@WebAppConfiguration,@ ContextConfiguration(classes = Initializer.class), @SpringBootConfiguration@ComponentScan,但它仍然会抛出同样的错误。 代码中没有其他配置类,如应用程序上下文。

1 个答案:

答案 0 :(得分:0)

您可以使用annotattion @SpringBootTest进行测试,使用属性&#34; classes&#34;,您需要添加spring-boot主类:

@SpringBootTest(classes = YOUR_MAIN_CLASS.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)