测试接口遇到问题

1报错java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

加上启动程序

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.CountDownLatch;

@SpringBootApplication

//@ComponentScan(value = {"test.test.test", "com.test.test"})  ComponentScan注解函数
//@MapperScan(basePackages = {"com.test.test"})  MapperScan注解函数
@Slf4j
public class ServiceBootstrap {

public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(ServiceBootstrap.class);
application.run(args);
log.info("启动成功!");
final CountDownLatch cdl = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
cdl.countDown();
}
});
cdl.await();
}
}

2 遇到

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)

注入的类中有未实现的接口,可以通过@Autowired(required=false)略过未实现接口

3遇到NullPointerException问题需要加上@RunWith(SpringRunner.class)

java.lang.NullPointerException
at com.zebra.carcloud.minioperationsrd.service.api.impl.****
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

spring自动注解的DAO是接口,没有具体实现,测试调用时需要在启动程序中添加详细的map和componentscan

@MapperScan(basePackages = {"com.test.mapper"})
@ComponentScan(basePackages = {"com.test1.common", "com.test2.service", "com.test3.message"})

4 打开一个新的项目后,遇到Error:java: java.lang.ExceptionInInitializerError

JDK版本不一致,换成1.8版本可以通过

原文地址:https://www.cnblogs.com/meadow/p/11012914.html