首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SpringRunner无法检测配置

SpringRunner无法检测配置
EN

Stack Overflow用户
提问于 2018-09-11 10:40:02
回答 2查看 34.6K关注 0票数 9

我有一个spring引导应用程序,它试图创建单元测试用例。下面是我试图运行的代码,我没有任何配置文件(只使用注释),所以加载所有配置的主类是ElastSearchBootApplication类。由于某种原因,我看到了下面的错误。

代码语言:javascript
运行
复制
@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() {
        ...

考试班:

代码语言:javascript
运行
复制
@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启动测试时,我看到了以下错误:

代码语言:javascript
运行
复制
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.
  1. 我看到@SpringBootTest用于集成测试,所以我可以将它用于单元测试吗?如果删除它,则会得到另一组类似的异常。如果不使用SpringBootTest,我会更感兴趣地运行这个测试案例。
  2. 为什么我的测试用例说缺少一些配置。在线上的示例讨论了我没有的xml文件。我在这里错过了什么?
  3. 我是否可以从fixedRate动态传递Environment的值,并将其放在@Scheduled(fixedRate = ${some.value.defined})

更新

我可以运行测试,但没有适当的命令。理想情况下,我希望setUp首先运行。但它的运行第二。另外,when(client.search(anyObject())).thenReturn(getResponse());的线路失败了,我不明白原因.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-11 11:01:39

您必须将注释@ContextConfiguration添加到您的测试类中,以指定配置文件。

代码语言:javascript
运行
复制
@ContextConfiguration(classes = ElastSearchBootApplication.class)
票数 20
EN

Stack Overflow用户

发布于 2018-09-11 12:06:22

试试这个:

代码语言:javascript
运行
复制
@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()));
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52274066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档