pkdevboxy / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Mocking interface method that accepts a varargs parameter does not return the mocked value as expected #494

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Consider the following interfaces and classes:

public interface FileUploadResult {
    public File getFile();
    public Throwable getThrowable();
}

public static class FileUploadResultImpl implements FileUploadResult {
    private final File file;
    private final Throwable throwable;

    public FileUploadResultImpl(final File file, final Throwable throwable) {
        this.file = file;
        this.throwable = throwable;
    }

    @Override
     public File getFile() {
        return this.file;
     }

    @Override
    public Throwable getThrowable() {
        return this.throwable;
    }
}

public interface FileUploader {
    public List<FileUploadResult> upload(final String stripPath, final File... files) throws FileUploaderException;
}

It is desired to mock the FileUploader interface as follows:

FileUploader fileUploader = mock(FileUploader.class);

List<FileUploadResult> fileUploadResults = new ArrayList<FileUploadResult>();
fileUploadResults.add(new FileUploadResultImpl(new File("foo"), null));
fileUploadResults.add(new FileUploadResultImpl(new File("bar"), null));

assertThat(fileUploadResults).hasSize(2);

when(fileUploader.upload(anyString(), (File[]) 
any())).thenReturn(fileUploadResults);

List<FileUploadResult> actualFileUploadResults = fileUploader.upload("foo", 
renderedResultsFiles);

assertThat(actualFileUploadResults).hasSize(2);

What is the expected output? What do you see instead?

I expect that the list actualFileUploadResults should have two elements in it, 
one for each of the file upload results that was added. Instead, the last 
assertion fails because the list is empty.

If I change the FileUploader interface definition as follows then the assertion 
succeeds:

public interface FileUploader {
    public List<FileUploadResult> upload(final String stripPath, final File[] files) throws FileUploaderException;
}

What version of the product are you using? On what operating system?

I am using mockito version 1.9.5 on Mac OS X 10.8.5.

Please provide any additional information below.

Output of java -version:

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

Original issue reported on code.google.com by jfitzpat...@gmail.com on 6 Jun 2014 at 2:53

GoogleCodeExporter commented 8 years ago
Hi,

Sorry for the late reply : `List<FileUploadResult> upload(final String 
stripPath, final File... files)` is using a vararag argument, one should use 
the dedicated `anyVararg` matcher.

http://docs.mockito.googlecode.com/hg/latest/org/mockito/Matchers.html#anyVararg
()

Cheers
Brice

Original comment by brice.du...@gmail.com on 27 Jun 2014 at 3:41