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.junit.runner.notification.Failure;
      8 import org.mockito.junit.MockitoJUnitRunner;
      9 import org.mockitousage.IMethods;
     10 import org.mockitoutil.TestBase;
     11 
     12 import static org.junit.Assert.assertEquals;
     13 import static org.mockito.Mockito.mock;
     14 import static org.mockito.Mockito.when;
     15 
     16 public class UnusedStubsExceptionMessageTest extends TestBase {
     17 
     18     //Moving the code around this class is tricky and may cause the test to fail
     19     //We're asserting on full exception message which contains line numbers
     20     //Let's leave it for now, updating the test is cheap and if it turns out hindrance we can make the assertion smarter.
     21     @RunWith(MockitoJUnitRunner.class)
     22     public static class HasUnnecessaryStubs {
     23         IMethods mock1 = when(mock(IMethods.class).simpleMethod(1)).thenReturn("1").getMock();
     24         IMethods mock2 = when(mock(IMethods.class).simpleMethod(2)).thenReturn("2").getMock();
     25         IMethods mock3 = when(mock(IMethods.class).simpleMethod(3)).thenReturn("3").getMock();
     26 
     27         @Test
     28         public void usesStub() {
     29             assertEquals("1", mock1.simpleMethod(1));
     30         }
     31 
     32         @Test
     33         public void usesStubWithDifferentArg() {
     34             assertEquals(null, mock2.simpleMethod(200));
     35             assertEquals(null, mock3.simpleMethod(300));
     36         }
     37     }
     38 
     39     @Test
     40     public void lists_all_unused_stubs_cleanly() {
     41         JUnitCore runner = new JUnitCore();
     42         //when
     43         Result result = runner.run(HasUnnecessaryStubs.class);
     44         //then
     45         Failure failure = result.getFailures().get(0);
     46         assertEquals("\n" +
     47                         "Unnecessary stubbings detected in test class: HasUnnecessaryStubs\n" +
     48                         "Clean & maintainable test code requires zero unnecessary code.\n" +
     49                         "Following stubbings are unnecessary (click to navigate to relevant line of code):\n" +
     50                         "  1. -> at org.mockitousage.junitrunner.UnusedStubsExceptionMessageTest$HasUnnecessaryStubs.<init>(UnusedStubsExceptionMessageTest.java:0)\n" +
     51                         "  2. -> at org.mockitousage.junitrunner.UnusedStubsExceptionMessageTest$HasUnnecessaryStubs.<init>(UnusedStubsExceptionMessageTest.java:0)\n" +
     52                         "Please remove unnecessary stubbings or use 'silent' option. More info: javadoc for UnnecessaryStubbingException class.",
     53                 filterLineNo(failure.getException().getMessage()));
     54     }
     55 }
     56