公司的一个项目采用了spring boot框架后。我做junit测试时报错。下面是代码,求解答~~。
CourseTest.java@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class CourseTest {
@Autowired
private CourseService courseService;
@Test
public void test(){
System.out.println("in.....");
String orgMemberId = "b38610ec63a3480a83bd51c953fff4ca";
List<CourseData> courseDtatList = null;
courseDtatList = courseService.findContractListServiceImpl(orgMemberId);
for(CourseData cd : courseDtatList){
System.out.println(cd.getCoachName());
}
}
}在spring boot 1.4中SpringApplicationConfiguration标记为过时了,所以官方就不建议这么使用了。我也是看到别人的博客:http://412887952-qq-com.iteye.com/blog/2317832
启动类代码:
StartApplication.java@SpringBootApplication
@ComponentScan(basePackages="com.wkllme")
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAspectJAutoProxy
@EnableConfigurationProperties(SystemProperty.class)
public class StartApplication extends WebMvcConfigurerAdapter implements ApplicationListener<ContextRefreshedEvent> 
{
@Resource(name="securityInterceptor")
private  HandlerInterceptor  securityInterceptor;
/**
 * 配置请求拦截器
 */
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(securityInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/pay/**");
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event)
{
ApplicationContext context=event.getApplicationContext();
if(context.getParent() != null )
return;
}
public static void main(String[] args)
{
System.out.println("in----------");
SpringApplication.run(StartApplication.class, args);
}
}控制台错误信息:14:43:46.806 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.wkllme.test.BaseTest].
14:43:46.835 [main] INFO org.springframework.test.context.TestContext - @ContextConfiguration not found for class [class com.wkllme.test.BaseTest]
14:43:46.844 [main] INFO org.springframework.test.context.TestContextManager - @TestExecutionListeners is not present for class [class com.wkllme.test.BaseTest]: using defaults.
14:43:46.869 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.wkllme.test.BaseTest]
14:43:46.870 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.wkllme.test.BaseTest]
14:43:46.883 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.wkllme.test.BaseTest]
14:43:46.883 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.wkllme.test.BaseTest]
14:43:46.884 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.wkllme.test.BaseTest]
14:43:46.884 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.wkllme.test.BaseTest]
14:43:46.885 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.wkllme.test.BaseTest]
14:43:46.885 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.wkllme.test.BaseTest]
14:43:46.886 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.wkllme.test.BaseTest]
14:43:46.887 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.wkllme.test.BaseTest]
14:43:46.891 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[TestContext@6be46e8f testClass = BaseTest, testInstance = com.wkllme.test.BaseTest@3567135c, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@327471b5 testClass = BaseTest, locations = '{}', classes = '{}', activeProfiles = '{}', contextLoader = 'null']]].
14:43:46.893 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4157f54e] to prepare test instance [com.wkllme.test.BaseTest@3567135c]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
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:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
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: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)
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:115)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 24 common frames omitted
14:43:46.897 [main] DEBUG org.springframework.test.context.support.DirtiesContextTestExecutionListener - After test class: context [[TestContext@6be46e8f testClass = BaseTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@327471b5 testClass = BaseTest, locations = '{}', classes = '{}', activeProfiles = '{}', contextLoader = 'null']]], dirtiesContext [false].我还在pom.xml中配置了:<properties>
<start-class>com.wkllme.wechat.controller.StartApplication</start-class> <!-- 指定启动类(main方法的位置) -->
</properties>求大家看一下。调了两天了 ~
我还换了另外的一种注解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={StartApplication.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
还是错误(写不下了只是主要报错信息):
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.event.internalEventListenerProcessor': .......
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransaction.........
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]: Factory method 'transactionAdvisor' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: .........
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionInt......