How to use TestableJUnitRunner method of org.mockito.junit.TestableJUnitRunner class

Best Mockito code snippet using org.mockito.junit.TestableJUnitRunner.TestableJUnitRunner

Source:StubbingWarningsJUnitRunnerTest.java Github

copy

Full Screen

...9import org.junit.runner.RunWith;10import org.mockito.Mock;11import org.mockito.exceptions.misusing.UnfinishedStubbingException;12import org.mockito.internal.util.SimpleMockitoLogger;13import org.mockito.junit.TestableJUnitRunner;14import org.mockitousage.IMethods;15import org.mockitoutil.TestBase;16import static org.junit.Assert.assertEquals;17import static org.mockito.Mockito.mock;18import static org.mockito.Mockito.when;19import static org.mockitoutil.JUnitResultAssert.assertThat;20public class StubbingWarningsJUnitRunnerTest extends TestBase {21 JUnitCore runner = new JUnitCore();22 SimpleMockitoLogger logger = TestableJUnitRunner.refreshedLogger();23 @Test public void no_arg_mismatch_warnings() {24 //when25 runner.run(PassingArgMismatch.class, FailingWithMatchingArgs.class, MismatchButStubAlreadyUsed.class);26 //then27 assertEquals("", filterLineNo(logger.getLoggedInfo()));28 }29 @Test public void shows_arg_mismatch_warnings_when_test_fails() {30 //when31 runner.run(FailingWithArgMismatch.class);32 //then33 assertEquals("[MockitoHint] FailingWithArgMismatch.test (see javadoc for MockitoHint):\n" +34 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithArgMismatch.test(StubbingWarningsJUnitRunnerTest.java:0)\n" +35 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithArgMismatch.test(StubbingWarningsJUnitRunnerTest.java:0)\n", filterLineNo(logger.getLoggedInfo()));36 }37 @Test public void shows_arg_mismatch_warnings_only_for_mismatches() {38 //when39 runner.run(FailingWithSomeStubMismatches.class);40 //then41 assertEquals("[MockitoHint] FailingWithSomeStubMismatches.test (see javadoc for MockitoHint):\n" +42 "[MockitoHint] 1. Unused... -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithSomeStubMismatches.test(StubbingWarningsJUnitRunnerTest.java:0)\n" +43 "[MockitoHint] ...args ok? -> at org.mockitousage.junitrunner.StubbingWarningsJUnitRunnerTest$FailingWithSomeStubMismatches.test(StubbingWarningsJUnitRunnerTest.java:0)\n", filterLineNo(logger.getLoggedInfo()));44 }45 @Test public void validates_mockito_usage() {46 //when47 Result result = runner.run(InvalidMockitoUsage.class);48 //then49 assertThat(result).fails(1, UnfinishedStubbingException.class);50 }51 @RunWith(TestableJUnitRunner.class)52 public static class PassingArgMismatch {53 IMethods mock = mock(IMethods.class);54 @Test public void test() throws Exception {55 when(mock.simpleMethod(1)).thenReturn("1");56 mock.simpleMethod(2);57 }58 }59 @RunWith(TestableJUnitRunner.class)60 public static class FailingWithArgMismatch {61 @Mock IMethods mock;62 @Test public void test() throws Exception {63 when(mock.simpleMethod(1)).thenReturn("1");64 mock.simpleMethod(2);65 throw new RuntimeException("x");66 }67 }68 @RunWith(TestableJUnitRunner.class)69 public static class FailingWithMatchingArgs {70 @Mock IMethods mock;71 @Test public void test() throws Exception {72 when(mock.simpleMethod(1)).thenReturn("1");73 mock.simpleMethod(1);74 throw new RuntimeException("x");75 }76 }77 @RunWith(TestableJUnitRunner.class)78 public static class FailingWithSomeStubMismatches {79 @Mock IMethods mock;80 @Test public void test() throws Exception {81 when(mock.simpleMethod(1)).thenReturn("1"); // <- used82 when(mock.simpleMethod(2)).thenReturn("2"); // <- unused83 mock.simpleMethod(1); // <- not reported84 mock.simpleMethod(3); // <- reported85 throw new RuntimeException("x");86 }87 }88 @RunWith(TestableJUnitRunner.class)89 public static class MismatchButStubAlreadyUsed {90 @Mock IMethods mock;91 @Test public void test() throws Exception {92 when(mock.simpleMethod(1)).thenReturn("1");93 mock.simpleMethod(1); // <-- used94 mock.simpleMethod(2); // <-- arg mismatch, but the stub was already used95 throw new RuntimeException("x");96 }97 }98 @RunWith(TestableJUnitRunner.class)99 public static class InvalidMockitoUsage {100 @Mock IMethods mock;101 @Test public void test() throws Exception {102 when(mock.simpleMethod()); // <-- unfinished stubbing103 }104 }105}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in TestableJUnitRunner

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful