English 中文(简体)
SpringRunner无法检测配置
原标题:SpringRunner unable to detect configuration

我有一个跳板申请,正试图创建单位试验场。 下面是我试图操作的守则,我没有任何组合文件(仅作说明),因此,所有组合的主要类别为<代码>。 ElastSearchBootApplication category。 出于某种原因,我发现以下错误。

@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()));
    }

我在尝试进行春季培训时看到以下错误:

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. 页: 1 测试用于一体化测试,因此我可以用于单位测试? 如果我去除,我会收到另一套例外,但看起来类似。 我更有兴趣在没有<代码>的情况下进行这一试验。 SpringBoot 测试

  2. 我的测试案例为什么说一些配置缺失。 这些样本在线谈到我没有的xml文档。 因此,我在这里失踪了吗?

  3. 我能动态地将<代码>固定的数值从Environment上移至@S subuled(固定的Rate = 美元---et. Value.fin})

<>>>>>

我可以进行试验,但没有适当的命令。 理想的情况是,将首先运行<代码>。 但其运作情况是第二次。 另外,当(客户)研究(任何目标(目标)))时,“

最佳回答

您在测试类别中添加“内容”一词,以具体说明组合档案。

@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()));
  }

注,在Junit5中,将“RunWith”(SpringRunner.class)改为“ExtendWith(Spring Extension.class)。 因此:

 @ExtendWith(SpringExtension.class)
 @SpringBootTest
 public class Your {
 ...




相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签