System.out.print() doesn't show anything in test methods

前端 未结 8 1295
春和景丽
春和景丽 2020-12-14 15:28

I\'m trying to print some data with System.out in my unit tests (@Test mehotds), but it is not showing anything. However, it works properly in

8条回答
  •  执念已碎
    2020-12-14 15:40

    I made i little trick in separate non-test class. It is not that smooth as logger, but if you are looking for quick solution in Spring Boot you can use this.

    PrintForTest.java

    import org.springframework.stereotype.Controller;
    
    @Controller
    public class PrintForTest {
    
        public static void print(String input){
            System.out.println(input);
        }
    }
    

    MainTest.java

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.junit.Assert;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class MainTest {
    
    ...
    
        @Test
        public void testingSomething(){
            PrintForTest.print("My new System.out.print()");
            Assert.assertEquals(...);
        }
    }
    

    edited: using static method, no need to use @Autowired.

提交回复
热议问题