Mockito nemůže zdá se, zesměšňovat rozhraní slf4j

0

Otázka

Snažím se, aby obal pro slf4j Logger rozhraní a vyzkoušet si to, ale pořád jsem dostat následující chybu.

TestDebugLog FAILED
    Wanted but not invoked:
    logger.debug("some message", "arg1", "arg2");
    -> at com.common.TrebuchetLoggerTest.TestDebugLog(TrebuchetLoggerTest.java:37)

    However, there was exactly 1 interaction with this mock:
    logger.debug("some message", "arg1", "arg2");
    -> at com.common.TrebuchetLogger.debug(TrebuchetLogger.java:19)
        at com.common.TrebuchetLoggerTest.TestDebugLog(TrebuchetLoggerTest.java:37)

1 test completed, 1 failed

Můj test vypadá

@RunWith(MockitoJUnitRunner.class)
public class TrebuchetLoggerTest {
  TrebuchetLogger trebuchetLogger;
  @Mock TrebuchetClient trebuchetClient;
  @Mock Logger logger;

  @Before
  public void setup() {
    trebuchetClient = mock(TrebuchetClient.class);
    when(trebuchetClient.launch(any(String.class))).thenReturn(true);
    logger = mock(Logger.class);
    trebuchetLogger = new TrebuchetLogger(trebuchetClient, logger);
  }

  @Test
  public void TestDebugLog() {
    trebuchetLogger.debug("debug-suffix", "some message", "arg1", "arg2");
    verify(logger).debug("some message", "arg1", "arg2");
  }
}

Můj kód je:

package com.common;

import org.slf4j.Logger;
import lombok.NonNull;
import com.trebuchet.client.TrebuchetClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
import org.slf4j.LoggerFactory;

@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class TrebuchetLogger {
  @NonNull private TrebuchetClient trebuchetClient;
  @NonNull private Logger log;

  public void debug(String trebuchetSuffix, String format, Object... arguments) {
    if (trebuchetClient.launch(trebuchetSuffix)) {
      log.debug(format, arguments);
    }
  }

  public void info(String trebuchetSuffix, String format, Object... arguments) {
    if (trebuchetClient.launch(trebuchetSuffix)) {
      log.info(format, arguments);
    }
  }

  public void warn(String trebuchetSuffix, String format, Object... arguments) {
    if (trebuchetClient.launch(trebuchetSuffix)) {
      log.warn(format, arguments);
    }
  }

  public void error(String trebuchetSuffix, String format, Object... arguments) {
    if (trebuchetClient.launch(trebuchetSuffix)) {
      log.error(format, arguments);
    }
  }
}

Proč můj mockito mock nezdaří s zdánlivě stejné výsledky v chybové zprávě?

mockito slf4j unit-testing
2021-11-24 06:13:55
1

Nejlepší odpověď

0

Voláte různé přetížení ve vašem SUT a ověřit.

// SUT
log.debug(format, arguments);

// Test
verify(logger).debug("some message", "arg1", "arg2");

odpovídají přetížení:

Pokud potřebujete zavolat varargs-přetížení, když máte formát a 2 argumenty, budete muset projít argumenty jako pole.

2021-11-24 06:51:21

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................