春のブートテスト – DEBUGログを停止する方法
Springブートテストを実行し、コンソールにDEBUGログがいっぱいで、 `application.yml`のログレベルを制御しようとしますが、単体テストでは設定を無視するだけです。
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;//...
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSequenceService {
@Autowired
SequenceService sequenceService;
@Test
public void testSequence() {
//...
}
}
コンソール
21:34:19.647[main]DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner ... 21:34:19.653[main]DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class ... 21:34:19.659[main]DEBUG org.springframework... 21:34:19.673[main]DEBUG org.springframework... 21:34:19.683[main]DEBUG org.springframework...//...
解決策
これを修正するには
src/test/resources`フォルダに
logback-test.xml`を作成します。

src/test/resources/logback-test.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework" level="ERROR"/>
<logger name="com.mkyong" level="DEBUG"/>
<logger name="org.mongodb" level="ERROR"/>
</configuration>