Home | History | Annotate | Download | only in junit
      1 /*
      2  * Copyright (c) 2017 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.internal.junit;
      6 
      7 import org.junit.Test;
      8 import org.mockito.internal.invocation.InvocationBuilder;
      9 import org.mockito.internal.util.SimpleMockitoLogger;
     10 import org.mockito.invocation.Invocation;
     11 import org.mockitoutil.TestBase;
     12 
     13 import static org.junit.Assert.assertEquals;
     14 import static org.junit.Assert.assertTrue;
     15 
     16 public class StubbingArgMismatchesTest extends TestBase {
     17 
     18     SimpleMockitoLogger logger = new SimpleMockitoLogger();
     19     StubbingArgMismatches mismatches = new StubbingArgMismatches();
     20 
     21     @Test
     22     public void no_op_when_no_mismatches() throws Exception {
     23         //when
     24         mismatches.format("MyTest.myTestMethod", logger);
     25 
     26         //then
     27         assertTrue(logger.isEmpty());
     28     }
     29 
     30     @Test
     31     public void logs_mismatch() throws Exception {
     32         //given
     33         mismatches.add(
     34                 new InvocationBuilder().args("a").location("-> at A.java").toInvocation(),
     35                 new InvocationBuilder().args("b").location("-> at B.java").toInvocation());
     36 
     37         //when
     38         mismatches.format("MyTest.myTestMethod", logger);
     39 
     40         //then
     41         assertEquals(
     42             "[MockitoHint] MyTest.myTestMethod (see javadoc for MockitoHint):\n" +
     43             "[MockitoHint] 1. Unused... -> at B.java\n" +
     44             "[MockitoHint]  ...args ok? -> at A.java\n", logger.getLoggedInfo());
     45     }
     46 
     47     @Test
     48     public void multiple_matching_invocations_per_stub_plus_some_other_invocation() throws Exception {
     49         //given
     50         Invocation stubbing = new InvocationBuilder().args("a").location("-> at A.java").toInvocation();
     51         mismatches.add(new InvocationBuilder().args("x").location("-> at X.java").toInvocation(), stubbing);
     52         mismatches.add(new InvocationBuilder().args("y").location("-> at Y.java").toInvocation(), stubbing);
     53 
     54         mismatches.add(
     55                 new InvocationBuilder().method("differentMethod").args("n").location("-> at N.java").toInvocation(),
     56                 new InvocationBuilder().method("differentMethod").args("m").location("-> at M.java").toInvocation());
     57 
     58         //when
     59         mismatches.format("MyTest.myTestMethod", logger);
     60 
     61         //then
     62         assertEquals(
     63             "[MockitoHint] MyTest.myTestMethod (see javadoc for MockitoHint):\n" +
     64             "[MockitoHint] 1. Unused... -> at A.java\n" +
     65             "[MockitoHint]  ...args ok? -> at X.java\n" +
     66             "[MockitoHint]  ...args ok? -> at Y.java\n" +
     67             "[MockitoHint] 2. Unused... -> at M.java\n" +
     68             "[MockitoHint]  ...args ok? -> at N.java\n", logger.getLoggedInfo());
     69     }
     70 }
     71