Correspondente de argumento extraviado detectado aqui. Você não pode usar correspondentes de argumento fora da verificação ou do stub no Mockito

Dos dois casos de teste a seguir, emBundleProcessorTest.java, estou ficando abaixo da exceção, embora meu primeiro caso de teste seja bem-sucedido.

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: correspondente de argumento extraviado detectado aqui:

-> em bundle.test.BundleProcessorTest.bundlePluginShouldNotBeNull (BundleProcessorTest.java:22)

Você não pode usar correspondentes de argumento fora da verificação ou stub. Exemplos de uso correto de correspondentes de argumento: when (mock.get (anyInt ())). ThenReturn (null); doThrow (new RuntimeException ()). when (mock) .someVoidMethod (anyObject ()); Verifique (mock) .someMethod (contém ("foo"))

Além disso, esse erro pode aparecer porque você usa correspondentes de argumento com métodos que não podem ser zombados. Seguintes métodosnão podes ser stubbed / verificado: final / private / equals () / hashCode ().

em bundle.test.BundleProcessorTest.bundlePluginCollectionShouldNotBeNull (BundleProcessorTest.java:28) em sun.reflect.NativeMethodAccessorImpl.invoke0 (método nativo) em sun.reflect.NativeMethodAccessorImpl.invoke (Unknown)

Veja abaixo a lista de códigos simplificados: -

BundlePlugin.java

package bundle;

import java.util.List;

public class BundlePlugin {

    private final String pluginName ;
    private final List<String> featureContent ;

    public BundlePlugin(String pluginName, List<String> featureContent) {
        super();
        this.pluginName = pluginName;
        this.featureContent = featureContent;
    }

    public String getPluginName() {
        return pluginName;
    }

    public List<String> getFeatureContent() {
        return featureContent;
    }
}

BundleProcessor.java

package bundle;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BundleProcessor {

    public BundlePlugin getBundlePlugin(String pluginName, Iterator<String> artifactIterator) {

        List<String> featureContent = new ArrayList<String>() ;

        return new BundlePlugin(pluginName, featureContent);
    }
}

BundleProcessorTest.java

package bundle.test;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;

import java.util.Iterator;
import java.util.List;

import org.junit.Test;

import bundle.BundleProcessor;

public class BundleProcessorTest {

    BundleProcessor bundleProcessor = new BundleProcessor() ;   

    @Test
    public void bundlePluginShouldNotBeNull() {

        Iterator<String> artifactIterator = mock(Iterator.class) ;
        bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) ;
        assertNotNull( bundlePlugin );
    }

    @Test
    public void bundlePluginContentShouldNotBeNull() {
        Iterator<String> artifactIterator = mock(Iterator.class) ;
        bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) ;

        List<String> featureContent = bundlePlugin.getFeatureContent() ;
        assertNotNull( featureContent );
    }
}

Como executar este teste sem problemas.

Editar 1:

Mas se eu marcar obundlePluginCollectionShouldNotBeNull teste com a anotação @Ignore e, em seguida, o primeiro caso de teste passa sem nenhuma exceção.

questionAnswers(1)

yourAnswerToTheQuestion