systemoutrule和调试堆栈问题

kh212irz  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(277)

嘿,我对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,测试方法=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',父级=[null]],属性=Mapempty,类用@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]调试org.springframework.test.context.cache-spring测试应用程序上下文缓存统计信息:[defaultcontextcache@455b6df1 size=1,maxsize=32,parentcontextcount=0,hitcount=2,misscont=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]调试org.springframework.test.context.cache-spring测试应用程序上下文缓存统计信息:[defaultcontextcache@455b6df1 size=1,maxsize=32,parentcontextcount=0,hitcount=3,misscont=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]调试org.springframework.test.context.cache-spring测试应用程序上下文缓存统计信息:[defaultcontextcache@455b6df1 size=1,maxsize=32,parentcontextcount=0,hitcount=4,我是防守队员
我做错什么了?

shyt4zoc

shyt4zoc1#

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

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

相关问题