ste*_*man 8 testing spring-boot
我正在使用 org.springframework.boot.test.OutputCapture 来测试记录某些内容的注释。
它对于单个测试非常有效,并且当单独运行测试时,如果源文件中存在使用输出捕获的多个测试,但是当多个测试一起运行时,只有第一个测试运行获取捕获的输出,其他测试运行获取空字符串。
有什么办法可以避免这种情况,我看不到任何配置的可能性吗?
这是一个简化的测试,说明了 import static org.hamcrest.Matchers 问题。; 导入静态 org.junit.Assert。;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.springframework.boot.test.OutputCapture;
/**
* Simplest implementation that illustrates my problem using {@link OutputCapture}.
* <p>
* If both tests are run, the second test run will not have any output captured so fails.
* If tests are run individually they both pass.
* <p>
* It is somehow related to
* 1) mocking with mockito which is not actually needed in this simplified implementation: without @InjectMocks no output is captured at all and both test fail.
* 2) if use System.out.println not log4j, both tests pass and output is captured without mocking as well.
*/
@FixMethodOrder // done to illustrate error
public class OutputCaptureTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
@InjectMocks
private InternalTestListener listener;// = new InternalTestListener();
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Test
public void test1() {
listener.doIt( 1 );
assertThat( outputCapture.toString() , containsString( "doIt "+1 ) );
}
@Test
public void test2() {
listener.doIt( 2 );
assertThat( outputCapture.toString() , containsString( "doIt "+2 ) );
}
static class InternalTestListener{
private static final Logger LOGGER = LogManager.getLogger( InternalTestListener.class );
public void doIt( int x ){
LOGGER.info( "doIt "+x );
// System.out.println( "doIt "+x );
}
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,我按照以下方式解决了它:
具体来说,缺少在控制台附加程序中添加的follow="true" :
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT" follow="true">
...
</Console>
Run Code Online (Sandbox Code Playgroud)
就我而言,正如@Andy Wilkinson 提到的,是一个配置问题。
| 归档时间: |
|
| 查看次数: |
5221 次 |
| 最近记录: |