SpringRunner无法检测配置
我有一个SpringBoot应用程序,我正在尝试为它创建单元测试用例。下面是我试图运行的代码,我没有任何配置文件(仅使用注释),因此加载所有配置的主类是SpringRunner无法检测配置,spring,spring-boot,spring-test,Spring,Spring Boot,Spring Test,我有一个SpringBoot应用程序,我正在尝试为它创建单元测试用例。下面是我试图运行的代码,我没有任何配置文件(仅使用注释),因此加载所有配置的主类是ElastSearchBootApplicationclass。出于某种原因,我看到下面的错误 @ComponentScan(basePackages = "com.somename") @SpringBootApplication @EnableScheduling public class ElastSearchBootApplication
ElastSearchBootApplication
class。出于某种原因,我看到下面的错误
@ComponentScan(basePackages = "com.somename")
@SpringBootApplication
@EnableScheduling
public class ElastSearchBootApplication {
private static final Logger LOG = LoggerFactory.getLogger(ElastSearchBootApplication.class);
public static void main(String[] args) {
SpringApplication.run(ElastSearchBootApplication.class, args);
}
@Autowired
private ElastSearchLogLevel logsSearch;
@Scheduled(fixedRate = 120000)
public void scheduledSearchLogs() {
...
测试等级:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ElastSearchBootApplication.class)
public class LogSearchTest {
@Mock
private RestHighLevelClient client;
@Mock
private ExecutorService ALERT_POOL;
@Before
public void setUp() throws Exception {
client = mock(RestHighLevelClient.class);
ALERT_POOL = mock(ExecutorService.class);
try {
when(client.search(anyObject())).thenReturn(getResponse());
} catch (Exception e) {
// I see NullPointerException but both the instances are available here
e.printStackTrace();
}
doNothing().when(ALERT_POOL.invokeAll(anyObject()));
}
我在尝试运行spring启动测试时看到以下错误:
org.springframework.boot.test.context.SpringBootTestContextBootstrapper buildDefaultMergedContextConfiguration
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.somename.search.LogSearchTest], using SpringBootContextLoader
org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
INFO: Could not detect default resource locations for test class [com.somename.search.LogSearchTest]: no resource found for suffixes {-context.xml, Context.groovy}.
org.springframework.test.context.support.AnnotationConfigContextLoaderUtils detectDefaultConfigurationClasses
INFO: Could not detect default configuration classes for test class [com.somename.search.LogSearchTest]: LogSearchTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
@SpringBootTest
用于集成测试,所以我可以将它用于单元测试吗?如果我删除它,那么我会得到另一组看起来类似的异常。如果没有SpringBootTest
Environment
动态传递fixedRate
的值,并将其像@Scheduled(fixedRate=${some.value.defined})
我可以运行测试,但没有正确的顺序。理想情况下,我希望先运行
setUp
。但它是第二名。还有一行when(client.search(anyObject()).thenReturn(getResponse())代码>失败,我不知道原因…您必须向测试类添加注释@ContextConfiguration以指定配置文件
@ContextConfiguration(classes = ElastSearchBootApplication.class)
试试这个:
@RunWith(SpringRunner.class)
@SpringBootTest
public class LogSearchTest {
@MockBean
private RestHighLevelClient client;
@MockBean
private ExecutorService ALERT_POOL;
@Before
public void setUp() throws Exception {
try {
when(client.search(anyObject())).thenReturn(getResponse());
} catch (Exception e) {
// I see NullPointerException but both the instances are available here
e.printStackTrace();
}
doNothing().when(ALERT_POOL.invokeAll(anyObject()));
}
我现在添加了它,它现在更进一步了,而不是从setup()
开始,它直接运行我的测试用例,尽管在它上面添加了@Before
。你在哪里添加了这个注释?不,junit@Before
不起作用,所以我在setup
中删除了代码,并将其放入测试方法中,这解决了我的一个问题。正如我所说的,try块中的代码出于某种原因引发异常,当代码进入elasticsearch API时,我得到了一个NullPointerException
这是可行的,但不确定为什么它不会检测groovy包。com.y.u.n.x.api.acceptance.tests.acceptance com.y.u.n.x.acceptance.tests.acceptance-->我的邮件类位于此处将测试类上的@RunWith
替换为@RunWith(SpringJUnit4ClassRunner.class)
。单元测试类不需要@ContextConfiguration
和@springbootest
。没有@ContextConfiguration(classes=elastsearchbootcomplication.class)
配置将不会被检测到。不过,我现在将更新代码,向您展示最新的。出于某些原因,我的模拟在时失败(client.search(anyObject())。然后返回(getResponse())代码>另外,您是否试图模拟elasticsearch客户端?这里说它不能被嘲笑,谢谢你的回复。以前,这是我开始讨论这个问题时的情况。实际上,我现在已经更新了代码。我认为这是SpringBootTest的问题,不知何故没有阅读test ENV,它应该先运行安装程序。如果你想使用@ContextConfiguration
,你应该创建类@Configuration
,在这个类中声明所有需要的bean:从主lcass@ComponentScan重复配置(basePackages=“com.somename”)
和@EnableScheduling
。出现错误“无法检测默认配置…”因为ElastSearchBootApplication
不是@Configuration
类。关于它的好话题:因为我添加了@ContextConfiguration
我的测试用例能够自动连接东西,它不需要单独的@Configuration
,我不需要@EnableScheduling
。如果你想最小化上下文,最好选择ice使用单独的配置类,否则,例如,您将通过@ComponentScan(basePackages=“com.somename”)
扫描整个软件包,而不只是创建一个测试所需的服务。您的问题的预期结果是什么?也许您在github上有示例,这样会更容易帮助您。