dhamini-poornachandra / mockito

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

Constructor injection with @InjectMocks is not working properly when injecting multiple mocks of the same type #422

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The problem?

Constructor injection with @InjectMocks is not working properly when injecting 
multiple mocks of the same type. Always single instance of given type is used.

What steps will reproduce the problem?

@RunWith(MockitoJUnitRunner.class)
public class ConsolePrinterTest {

    @Mock
    PrintStream outStream;

    @Mock
    PrintStream errStream;

    @InjectMocks
    ConsolePrinter classUnderTest;

    @Test
    public void testPrinting() {

        classUnderTest.println("test msg");
        verify(outStream).println("test msg");
    }

    public static class ConsolePrinter {

        private final PrintStream out;
        private final PrintStream err;

        public ConsolePrinter(final PrintStream outStream, final PrintStream errStream) {
            this.out = outStream;
            this.err = errStream;
        }

        public void println(final String str) {
            out.println(str);
        }

        public void printlnError(final String str) {
            err.println(str);
        }

    }

}

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

If properly injected, the test should always be green. Instead, it is sometimes 
red and sometimes green, depending on which mock was used as both of 
contructor's arguments.

What version of the product are you using? On what operating system?
mockito 1.9.5; Java 1.7.0_13; Linux 3.6.11

Original issue reported on code.google.com by marius.g...@gmail.com on 16 Feb 2013 at 10:13

GoogleCodeExporter commented 8 years ago
Hi,

You can use the mock name annotation attribute, when there is two Object of the 
same type. Otherwise it is not possible to detect automagically the right mocks 
for the right properties.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 14 Mar 2013 at 7:44

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 29 Apr 2013 at 4:18