Home | History | Annotate | Download | only in junitrunner
      1 package org.mockitousage.junitrunner;
      2 
      3 import org.junit.Test;
      4 import org.junit.runner.JUnitCore;
      5 import org.junit.runner.Result;
      6 import org.junit.runner.RunWith;
      7 import org.mockito.Mock;
      8 import org.mockito.exceptions.misusing.UnfinishedStubbingException;
      9 import org.mockito.internal.util.SimpleMockitoLogger;
     10 import org.mockito.junit.TestableJUnitRunner;
     11 import org.mockitousage.IMethods;
     12 import org.mockitoutil.TestBase;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 import static org.mockito.Mockito.mock;
     16 import static org.mockito.Mockito.when;
     17 import static org.mockitoutil.JUnitResultAssert.assertThat;
     18 
     19 public class StubbingWarningsJUnitRunnerTest extends TestBase {
     20 
     21     JUnitCore runner = new JUnitCore();
     22     SimpleMockitoLogger logger = TestableJUnitRunner.refreshedLogger();
     23 
     24     @Test public void no_arg_mismatch_warnings() {
     25         //when
     26         runner.run(PassingArgMismatch.class, FailingWithMatchingArgs.class, MismatchButStubAlreadyUsed.class);
     27 
     28         //then
     29         assertEquals("", filterLineNo(logger.getLoggedInfo()));
     30     }
     31 
     32     @Test public void shows_arg_mismatch_warnings_when_test_fails() {
     33         //when
     34         runner.run(FailingWithArgMismatch.class);
     35 
     36         //then
     37         assertEquals("[MockitoHint] FailingWithArgMismatch.test (see javadoc for MockitoHint):\n" +
     38                 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithArgMismatch.test(StubbingWarningsJUnitRunnerTest.java:0)\n" +
     39                 "[MockitoHint]  ...args ok? -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithArgMismatch.test(StubbingWarningsJUnitRunnerTest.java:0)\n", filterLineNo(logger.getLoggedInfo()));
     40     }
     41 
     42     @Test public void shows_arg_mismatch_warnings_only_for_mismatches() {
     43         //when
     44         runner.run(FailingWithSomeStubMismatches.class);
     45 
     46         //then
     47         assertEquals("[MockitoHint] FailingWithSomeStubMismatches.test (see javadoc for MockitoHint):\n" +
     48                 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithSomeStubMismatches.test(StubbingWarningsJUnitRunnerTest.java:0)\n" +
     49                 "[MockitoHint]  ...args ok? -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithSomeStubMismatches.test(StubbingWarningsJUnitRunnerTest.java:0)\n", filterLineNo(logger.getLoggedInfo()));
     50     }
     51 
     52     @Test public void validates_mockito_usage() {
     53         //when
     54         Result result = runner.run(InvalidMockitoUsage.class);
     55 
     56         //then
     57         assertThat(result).fails(1, UnfinishedStubbingException.class);
     58     }
     59 
     60     @RunWith(TestableJUnitRunner.class)
     61     public static class PassingArgMismatch {
     62         IMethods mock = mock(IMethods.class);
     63         @Test public void test() throws Exception {
     64             when(mock.simpleMethod(1)).thenReturn("1");
     65             mock.simpleMethod(2);
     66         }
     67     }
     68 
     69     @RunWith(TestableJUnitRunner.class)
     70     public static class FailingWithArgMismatch {
     71         @Mock IMethods mock;
     72         @Test public void test() throws Exception {
     73             when(mock.simpleMethod(1)).thenReturn("1");
     74             mock.simpleMethod(2);
     75             throw new RuntimeException("x");
     76         }
     77     }
     78 
     79     @RunWith(TestableJUnitRunner.class)
     80     public static class FailingWithMatchingArgs {
     81         @Mock IMethods mock;
     82         @Test public void test() throws Exception {
     83             when(mock.simpleMethod(1)).thenReturn("1");
     84             mock.simpleMethod(1);
     85             throw new RuntimeException("x");
     86         }
     87     }
     88 
     89     @RunWith(TestableJUnitRunner.class)
     90     public static class FailingWithSomeStubMismatches {
     91         @Mock IMethods mock;
     92         @Test public void test() throws Exception {
     93             when(mock.simpleMethod(1)).thenReturn("1"); // <- used
     94             when(mock.simpleMethod(2)).thenReturn("2"); // <- unused
     95 
     96             mock.simpleMethod(1); // <- not reported
     97             mock.simpleMethod(3); // <- reported
     98 
     99             throw new RuntimeException("x");
    100         }
    101     }
    102 
    103     @RunWith(TestableJUnitRunner.class)
    104     public static class MismatchButStubAlreadyUsed {
    105         @Mock IMethods mock;
    106         @Test public void test() throws Exception {
    107             when(mock.simpleMethod(1)).thenReturn("1");
    108             mock.simpleMethod(1); // <-- used
    109             mock.simpleMethod(2); // <-- arg mismatch, but the stub was already used
    110 
    111             throw new RuntimeException("x");
    112         }
    113     }
    114 
    115     @RunWith(TestableJUnitRunner.class)
    116     public static class InvalidMockitoUsage {
    117         @Mock IMethods mock;
    118         @Test public void test() throws Exception {
    119             when(mock.simpleMethod()); // <-- unfinished stubbing
    120         }
    121     }
    122 }
    123