【问题标题】:Problem with SystemOutRule and DEBUG stackSystemOutRule 和 DEBUG 堆栈的问题
【发布时间】:2021-04-13 11:30:18
【问题描述】:

嘿,我在 JUnit (4.12) 中的 SystemOutRule 日志和测试有问题。

我有简单的测试类:

package com.example.demo;

import config.FootballerConfig;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = FootballerConfig.class)
public class FootballerConfigTest {

    @Rule
    public final SystemOutRule log = new SystemOutRule().enableLog();

    @Autowired
    private FootballerStats footballerStats;

    @Autowired
    private Footballer footballer;

    @Test
    public void footballerShouldNotBeNull() {
        assertNotNull(footballer);
    }

    @Test
    public void getPosition() {
        footballerStats.getPosition();
        Assert.assertEquals("I'm playing as defender", log.getLogWithNormalizedLineSeparator().trim());
    }
}

我期望的输出是:“我正在扮演防守者”,但 SystemOutRule 给出了一个完整的跟踪堆栈,例如:

18:37:32.501 [main] 调试 org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener

  • 测试方法之前:上下文 [DefaultTestContext@7ce3cb8e testClass = FootballerConfigTest,testInstance = com.example.demo.FootballerConfigTest@1139b2f3,testMethod = getPosition@FootballerConfigTest,testException = [null],mergedContextConfiguration = [MergedContextConfiguration@78b66d36 testClass = FootballerConfigTest,位置 = '{}',类 = '{class config.FootballerConfig}',contextInitializerClasses = '[]',activeProfiles = '{}',propertySourceLocations = '{}',propertySourceProperties = '{}',contextCustomizers = set[ org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@51931956, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@51b7e5df, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org. springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@45f45fa1, org.springframework.boot.tes t.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@16aa0a0a], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', 父 = [ null]],attributes = map[[empty]]],用@DirtiesContext [false]注释的类,模式[null],用@DirtiesContext [false]注释的方法,模式[null]。 18:37:32.514 [main] 调试 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
  • 从缓存中检索 ApplicationContext [1233990028],键为 [[MergedContextConfiguration@78b66d36 testClass = FootballerConfigTest,locations = '{}',classes = '{class config.FootballerConfig}',contextInitializerClasses = '[]',activeProfiles = '{ }',propertySourceLocations = '{}',propertySourceProperties = '{}',contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@51931956,org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer @51b7e5df, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@45f45fa1, org.springframework.boot.test.autoconfigure.properties .PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@16aa0a0a], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]] 18:37:32.514 [main] DEBUG org.springframework.test.context.cache - Spring 测试 ApplicationContext 缓存统计:[DefaultContextCache@455b6df1 size = 1,maxSize = 32,parentContextCount = 0,hitCount = 2,missCount = 1] 18:37:32.516 [main] 调试 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
  • 从缓存中检索 ApplicationContext [1233990028],键为 [[MergedContextConfiguration@78b66d36 testClass = FootballerConfigTest,locations = '{}',classes = '{class config.FootballerConfig}',contextInitializerClasses = '[]',activeProfiles = '{ }',propertySourceLocations = '{}',propertySourceProperties = '{}',contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@51931956,org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer @51b7e5df, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@45f45fa1, org.springframework.boot.test.autoconfigure.properties .PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@16aa0a0a], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]] 18:37:32.516 [main] DEBUG org.springframework.test.context.cache - Spring 测试 ApplicationContext 缓存统计:[DefaultContextCache@455b6df1 size = 1,maxSize = 32,parentContextCount = 0,hitCount = 3,missCount = 1] 18:37:33.290 [main] 调试 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
  • 从缓存中检索 ApplicationContext [1233990028],键为 [[MergedContextConfiguration@78b66d36 testClass = FootballerConfigTest,locations = '{}',classes = '{class config.FootballerConfig}',contextInitializerClasses = '[]',activeProfiles = '{ }',propertySourceLocations = '{}',propertySourceProperties = '{}',contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@51931956,org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer @51b7e5df, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@45f45fa1, org.springframework.boot.test.autoconfigure.properties .PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@16aa0a0a], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]] 18:37:33.290 [main] DEBUG org.springframework.test.context.cache - Spring 测试 ApplicationContext 缓存统计:[DefaultContextCache@455b6df1 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 4, missCount = 1] 我是防守球员

我做错了什么?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    好的。我做到了。在 getPosition() 方法中使用 log.clear() 就足够了。

    @Test
    public void getPosition() {
        log.clearLog();
        footballerStats.getPosition();
        Assert.assertEquals("I'm playing as defender\n", log.getLogWithNormalizedLineSeparator());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-09
      • 2021-09-02
      • 2020-04-12
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2011-03-17
      • 2011-01-23
      相关资源
      最近更新 更多