Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hey i have problem with SystemOutRule log and test in JUnit (4.12).

I have simple Test class:

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

The output I expect is: "I'm playing as defender" but SystemOutRule gives all a whole track stack like:

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

  • Before test method: context [DefaultTestContext@7ce3cb8e testClass = FootballerConfigTest, testInstance = com.example.demo.FootballerConfigTest@1139b2f3, testMethod = getPosition@FootballerConfigTest, testException = [null], mergedContextConfiguration = [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]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null]. 18:37:32.514 [main] DEBUG org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
  • Retrieved ApplicationContext [1233990028] from cache with key [[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 test ApplicationContext cache statistics: [DefaultContextCache@455b6df1 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 2, missCount = 1] 18:37:32.516 [main] DEBUG org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
  • Retrieved ApplicationContext [1233990028] from cache with key [[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 test ApplicationContext cache statistics: [DefaultContextCache@455b6df1 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 3, missCount = 1] 18:37:33.290 [main] DEBUG org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
  • Retrieved ApplicationContext [1233990028] from cache with key [[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 test ApplicationContext cache statistics: [DefaultContextCache@455b6df1 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 4, missCount = 1] I'm playing as defender

What am I doing wrong?

question from:https://stackoverflow.com/questions/65617453/problem-with-systemoutrule-and-debug-stack

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
104 views
Welcome To Ask or Share your Answers For Others

1 Answer

OK. I did it. It was enough to use log.clear () in the getPosition() method.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...