相关文章推荐
发呆的春卷  ·  spark ...·  5 月前    · 
体贴的小刀  ·  C++ Socket超时设置 - ...·  1 年前    · 
微笑的汉堡包  ·  mysql json 不为空-掘金·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a class that I mocked using Mockito. The original class is quite simple such as the following.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyNewClass{
    private static Logger logger = LoggerFactory.getLogger(MyNewClass.class);
     * Only for unit/integration test, otherwise no need to use this method.
    protected static void setLogger(Logger logger) {
        MyNewClass.logger = logger;

The method logger.info("SomeString") will print to a file since it is configured for production. In my test class how do I print to a console instead? I'm currently in Eclipse.

public class MyNewClassTest{
    private static Logger mockedLogger = Mockito.mock(Logger.class);
    @Before
    public void setUp() throws Exception {
        MyNewClass.setLogger(mockedLogger);
        // TODO how to print to console instead of file.

Then how do I overwrite the mockedLogger.info("SomeString") to print to console , simply by using System.out.println()

Inject the logger, don't construct it statically. Tools like PowerMock should only be used as a last resort since they utilize byte code manipulation. Also, you can reconfigure the logger for tests. – Turing85 Aug 24, 2020 at 18:51

Honestly, your approach seems to me a bit odd: I'd rather define separate logging configuration for tests. Anyways, If you have good reasons for what you are trying to achieve, here is how you can do it:

@Before
public void setUp() {
    MyNewClass.setLogger(mockedLogger);
    Mockito.doAnswer(invocation -> {
        System.out.println((String) invocation.getArgument(0));
        return null;
    }).when(mockedLogger).info(anyString());

Using PowerMock, you could get rid of the logger setter in the MyNewClass by mocking LoggerFactory:

@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggerFactory.class)
public class MyNewClassTest {
    private static Logger mockedLogger = Mockito.mock(Logger.class);
    @Before
    public void setUp() {
        PowerMockito.mockStatic(LoggerFactory.class);
        PowerMockito.when(LoggerFactory.getLogger(any(Class.class))).thenReturn(mockedLogger);
        Mockito.doAnswer(invocation -> {
            System.out.println((String) invocation.getArgument(0));
            return null;
        }).when(mockedLogger).info(anyString());
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.