Spring 项目进行测试的时候提示 @SpringBootConfiguration 没有配置

提示的配置信息为:

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

	at org.springframework.util.Assert.state(Assert.java:76)
	at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:236)
	at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:152)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:393)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:309)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:262)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:107)

看提示上说的是没有找到配置属性。

问题和解决

如果我们确定已经配置了 Spring ,但是测试没有办法测试的话,主要原因是启动文件和测试文件的包不在一个路径下。

针对我们的项目,我们 Application 的路径在:

package com.usvisatrack.data;

但是我们这个测试文件的路径在:

package com.usvisatrack.service;

这样的配置是没有办法让测试运行的。

解决办法就是将路径调整下,将这个测试文件放到:

package com.usvisatrack.data.tests;

路径下就可以了。

如上图更新后的显示。