今天在使用springboot 测试的时候时遇到了一个很坑的问题,自己明明注入了bean,但就是拿不到.情况如下:
于是自己在controller测试看看到底注入了没,打开浏览器,发现确实注入了啊
那咋测试时获取的偏偏是null. 我怀疑自己是不是没有使用上spring的测试模块,或者说@SpringbootTest注解失效了,反正就是springboot测试 没有正常执行.后来我才明白,由于自己的项目不是使用Spring Initializr创建的,是不是因为找不到主测试类无法注入,直接当做对象声明了,所以为才null.
于是我又打开正常的Spring Initializr创建的项目,在test包下做了一个验证.发现如果不在主测试类所在包及其子包下,可能直接报错,test包结构和错误如下:
于是自己手动修改为@SpringBootTest(classes = {主启动类.class}),标明要测试的是@SpringbootApplication注解的类.修改后测试正常.
于是点开@SpringbootTest,部分源码如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
public @interface SpringBootTest {
/**
* The <em>component classes</em> to use for loading an
* {@link org.springframework.context.ApplicationContext ApplicationContext}. Can also
* be specified using
* {@link ContextConfiguration#classes() @ContextConfiguration(classes=...)}. If no
* explicit classes are defined the test will look for nested
* {@link Configuration @Configuration} classes, before falling back to a
* {@link SpringBootConfiguration @SpringBootConfiguration} search.
* @see ContextConfiguration#classes()
* @return the component classes used to load the application context
*/
Class<?>[] classes() default {};
这个注解的classes()方法是干什么的,人家注解的一清二楚.大概意思说,它会加载ApplicationContext也就是测试的上下文环境.默认为{}.当然也能明确指定要加载的是哪个类作为测试的上下文.如果没有指定,会搜索@configuration注解的的类进而搜寻@SpringBootConfiguration标注的类.最后返回所有的类组件作为加载的测试环境.
所以如果我们的测试类没有位于主测试类所在包及其子包下,那么就搜索不到,当然就没有spring的测试环境,当然也无法自动注入.
另外对于不是使用initializer创建的spingboot项目,有可能出现结构不完整什么的情况.对于随便建的测试类,test下测试时不报错也得不到正常结果,即使加上@SpringBootTest(classes = {App.class})还是不行,可以尝试再加上@RunWith(SpringRunner.class).对于这种情况,建议如下写法:
@SpringBootTest(classes = {App.class})
@RunWith(SpringRunner.class)
我第一次遇到的可能就是这种情况,修改后再测试一切正常: