Best Mockito code snippet using org.mockitousage.stubbing.StubbingWithThrowablesTest.ExpectedException.none
Source:StubbingWithThrowablesTest.java
1/**2 * Copyright (c) 2007 Mockito contributors3 * This program is made available under the terms of the MIT License.4 */5package org.mockitousage.stubbing;6import java.io.IOException;7import java.io.Reader;8import java.util.LinkedList;9import java.util.Map;10import org.assertj.core.api.Assertions;11import org.assertj.core.api.ThrowableAssert;12import org.hamcrest.CoreMatchers;13import org.junit.Assert;14import org.junit.Rule;15import org.junit.Test;16import org.junit.rules.ExpectedException;17import org.mockito.Mockito;18import org.mockito.exceptions.base.MockitoException;19import org.mockito.exceptions.verification.NoInteractionsWanted;20import org.mockito.exceptions.verification.WantedButNotInvoked;21import org.mockitousage.IMethods;22import org.mockitoutil.TestBase;23@SuppressWarnings({ "serial", "unchecked", "rawtypes" })24public class StubbingWithThrowablesTest extends TestBase {25 private LinkedList mock;26 private Map mockTwo;27 @Rule28 public ExpectedException exception = ExpectedException.none();29 @Test30 public void throws_same_exception_consecutively() {31 Mockito.when(mock.add("")).thenThrow(new StubbingWithThrowablesTest.ExceptionOne());32 // 1st invocation33 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {34 public void call() {35 mock.add("");36 }37 }).isInstanceOf(StubbingWithThrowablesTest.ExceptionOne.class);38 mock.add("1");39 // 2nd invocation40 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {41 public void call() {42 mock.add("");43 }44 }).isInstanceOf(StubbingWithThrowablesTest.ExceptionOne.class);45 }46 @Test47 public void throws_same_exception_consecutively_with_doThrow() {48 Mockito.doThrow(new StubbingWithThrowablesTest.ExceptionOne()).when(mock).clear();49 // 1st invocation50 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {51 public void call() {52 mock.clear();53 }54 }).isInstanceOf(StubbingWithThrowablesTest.ExceptionOne.class);55 mock.add("1");56 // 2nd invocation57 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {58 public void call() {59 mock.clear();60 }61 }).isInstanceOf(StubbingWithThrowablesTest.ExceptionOne.class);62 }63 @Test64 public void shouldStubWithThrowable() throws Exception {65 IllegalArgumentException expected = new IllegalArgumentException("thrown by mock");66 Mockito.when(mock.add("throw")).thenThrow(expected);67 exception.expect(CoreMatchers.sameInstance(expected));68 mock.add("throw");69 }70 @Test71 public void shouldSetThrowableToVoidMethod() throws Exception {72 IllegalArgumentException expected = new IllegalArgumentException("thrown by mock");73 Mockito.doThrow(expected).when(mock).clear();74 exception.expect(CoreMatchers.sameInstance(expected));75 mock.clear();76 }77 @Test78 public void shouldLastStubbingVoidBeImportant() throws Exception {79 Mockito.doThrow(new StubbingWithThrowablesTest.ExceptionOne()).when(mock).clear();80 Mockito.doThrow(new StubbingWithThrowablesTest.ExceptionTwo()).when(mock).clear();81 exception.expect(StubbingWithThrowablesTest.ExceptionTwo.class);82 mock.clear();83 }84 @Test85 public void shouldFailStubbingThrowableOnTheSameInvocationDueToAcceptableLimitation() throws Exception {86 Mockito.when(mock.size()).thenThrow(new StubbingWithThrowablesTest.ExceptionOne());87 exception.expect(StubbingWithThrowablesTest.ExceptionOne.class);88 Mockito.when(mock.size()).thenThrow(new StubbingWithThrowablesTest.ExceptionTwo());89 }90 @Test91 public void shouldAllowSettingCheckedException() throws Exception {92 Reader reader = Mockito.mock(Reader.class);93 IOException ioException = new IOException();94 Mockito.when(reader.read()).thenThrow(ioException);95 exception.expect(CoreMatchers.sameInstance(ioException));96 reader.read();97 }98 @Test99 public void shouldAllowSettingError() throws Exception {100 Error error = new Error();101 Mockito.when(mock.add("quake")).thenThrow(error);102 exception.expect(Error.class);103 mock.add("quake");104 }105 @Test106 public void shouldNotAllowNullExceptionType() {107 exception.expect(MockitoException.class);108 exception.expectMessage("Cannot stub with null throwable");109 Mockito.when(mock.add(null)).thenThrow(((Exception) (null)));110 }111 @Test112 public void shouldInstantiateExceptionClassOnInteraction() {113 Mockito.when(mock.add(null)).thenThrow(StubbingWithThrowablesTest.NaughtyException.class);114 exception.expect(StubbingWithThrowablesTest.NaughtyException.class);115 mock.add(null);116 }117 @Test118 public void shouldInstantiateExceptionClassWithOngoingStubbingOnInteraction() {119 Mockito.doThrow(StubbingWithThrowablesTest.NaughtyException.class).when(mock).add(null);120 exception.expect(StubbingWithThrowablesTest.NaughtyException.class);121 mock.add(null);122 }123 @Test124 public void shouldNotAllowSettingInvalidCheckedException() {125 exception.expect(MockitoException.class);126 exception.expectMessage("Checked exception is invalid for this method");127 Mockito.when(mock.add("monkey island")).thenThrow(new Exception());128 }129 @Test130 public void shouldNotAllowSettingNullThrowable() {131 exception.expect(MockitoException.class);132 exception.expectMessage("Cannot stub with null throwable");133 Mockito.when(mock.add("monkey island")).thenThrow(((Throwable) (null)));134 }135 @Test136 public void shouldNotAllowSettingNullThrowableArray() {137 exception.expect(MockitoException.class);138 exception.expectMessage("Cannot stub with null throwable");139 Mockito.when(mock.add("monkey island")).thenThrow(((Throwable[]) (null)));140 }141 @Test142 public void shouldNotAllowSettingNullThrowableClass() {143 exception.expect(MockitoException.class);144 exception.expectMessage("Exception type cannot be null");145 Mockito.when(mock.isEmpty()).thenThrow(((Class) (null)));146 }147 @Test148 public void shouldNotAllowSettingNullThrowableClasses() {149 exception.expect(MockitoException.class);150 exception.expectMessage("Exception type cannot be null");151 Mockito.when(mock.isEmpty()).thenThrow(RuntimeException.class, ((Class[]) (null)));152 }153 @Test154 public void shouldNotAllowSettingNullVarArgThrowableClass() {155 exception.expect(MockitoException.class);156 exception.expectMessage("Exception type cannot be null");157 Mockito.when(mock.isEmpty()).thenThrow(RuntimeException.class, ((Class) (null)));158 }159 @Test160 public void doThrowShouldNotAllowSettingNullThrowableClass() {161 exception.expect(MockitoException.class);162 exception.expectMessage("Exception type cannot be null");163 Mockito.doThrow(((Class) (null))).when(mock).isEmpty();164 }165 @Test166 public void doThrowShouldNotAllowSettingNullThrowableClasses() throws Exception {167 exception.expect(MockitoException.class);168 exception.expectMessage("Exception type cannot be null");169 Mockito.doThrow(RuntimeException.class, ((Class) (null))).when(mock).isEmpty();170 }171 @Test172 public void doThrowShouldNotAllowSettingNullVarArgThrowableClasses() throws Exception {173 exception.expect(MockitoException.class);174 exception.expectMessage("Exception type cannot be null");175 Mockito.doThrow(RuntimeException.class, ((Class[]) (null))).when(mock).isEmpty();176 }177 @Test178 public void shouldNotAllowSettingNullVarArgsThrowableClasses() throws Exception {179 exception.expect(MockitoException.class);180 exception.expectMessage("Exception type cannot be null");181 Mockito.when(mock.isEmpty()).thenThrow(RuntimeException.class, ((Class<RuntimeException>[]) (null)));182 }183 @Test184 public void shouldNotAllowDifferntCheckedException() throws Exception {185 IMethods mock = Mockito.mock(IMethods.class);186 exception.expect(MockitoException.class);187 exception.expectMessage("Checked exception is invalid for this method");188 Mockito.when(mock.throwsIOException(0)).thenThrow(StubbingWithThrowablesTest.CheckedException.class);189 }190 @Test191 public void shouldNotAllowCheckedExceptionWhenErrorIsDeclared() throws Exception {192 IMethods mock = Mockito.mock(IMethods.class);193 exception.expect(MockitoException.class);194 exception.expectMessage("Checked exception is invalid for this method");195 Mockito.when(mock.throwsError(0)).thenThrow(StubbingWithThrowablesTest.CheckedException.class);196 }197 @Test198 public void shouldNotAllowCheckedExceptionWhenNothingIsDeclared() throws Exception {199 IMethods mock = Mockito.mock(IMethods.class);200 exception.expect(MockitoException.class);201 exception.expectMessage("Checked exception is invalid for this method");202 Mockito.when(mock.throwsNothing(true)).thenThrow(StubbingWithThrowablesTest.CheckedException.class);203 }204 @Test205 public void shouldMixThrowablesAndReturnsOnDifferentMocks() throws Exception {206 Mockito.when(mock.add("ExceptionOne")).thenThrow(new StubbingWithThrowablesTest.ExceptionOne());207 Mockito.when(mock.getLast()).thenReturn("last");208 Mockito.doThrow(new StubbingWithThrowablesTest.ExceptionTwo()).when(mock).clear();209 Mockito.doThrow(new StubbingWithThrowablesTest.ExceptionThree()).when(mockTwo).clear();210 Mockito.when(mockTwo.containsValue("ExceptionFour")).thenThrow(new StubbingWithThrowablesTest.ExceptionFour());211 Mockito.when(mockTwo.get("Are you there?")).thenReturn("Yes!");212 Assert.assertNull(mockTwo.get("foo"));213 Assert.assertTrue(mockTwo.keySet().isEmpty());214 Assert.assertEquals("Yes!", mockTwo.get("Are you there?"));215 try {216 mockTwo.clear();217 Assert.fail();218 } catch (StubbingWithThrowablesTest.ExceptionThree e) {219 }220 try {221 mockTwo.containsValue("ExceptionFour");222 Assert.fail();223 } catch (StubbingWithThrowablesTest.ExceptionFour e) {224 }225 Assert.assertNull(mock.getFirst());226 Assert.assertEquals("last", mock.getLast());227 try {228 mock.add("ExceptionOne");229 Assert.fail();230 } catch (StubbingWithThrowablesTest.ExceptionOne e) {231 }232 try {233 mock.clear();234 Assert.fail();235 } catch (StubbingWithThrowablesTest.ExceptionTwo e) {236 }237 }238 @Test239 public void shouldStubbingWithThrowableBeVerifiable() {240 Mockito.when(mock.size()).thenThrow(new RuntimeException());241 Mockito.doThrow(new RuntimeException()).when(mock).clone();242 try {243 mock.size();244 Assert.fail();245 } catch (RuntimeException e) {246 }247 try {248 mock.clone();249 Assert.fail();250 } catch (RuntimeException e) {251 }252 Mockito.verify(mock).size();253 Mockito.verify(mock).clone();254 Mockito.verifyNoMoreInteractions(mock);255 }256 @Test257 public void shouldStubbingWithThrowableFailVerification() {258 Mockito.when(mock.size()).thenThrow(new RuntimeException());259 Mockito.doThrow(new RuntimeException()).when(mock).clone();260 Mockito.verifyZeroInteractions(mock);261 mock.add("test");262 try {263 Mockito.verify(mock).size();264 Assert.fail();265 } catch (WantedButNotInvoked e) {266 }267 try {268 Mockito.verify(mock).clone();269 Assert.fail();270 } catch (WantedButNotInvoked e) {271 }272 try {273 Mockito.verifyNoMoreInteractions(mock);274 Assert.fail();275 } catch (NoInteractionsWanted e) {276 }277 }278 private class ExceptionOne extends RuntimeException {}279 private class ExceptionTwo extends RuntimeException {}280 private class ExceptionThree extends RuntimeException {}281 private class ExceptionFour extends RuntimeException {}282 private class CheckedException extends Exception {}283 public class NaughtyException extends RuntimeException {284 public NaughtyException() {285 throw new RuntimeException("boo!");286 }287 }288}...
ExpectedException.none
Using AI Code Generation
1 public void should_not_allow_nulls() {2 TestBase testBase = new TestBase();3 testBase.test();4 exception.expect(NullPointerException.class);5 exception.expectMessage("should not allow nulls");6 }7 public void should_not_allow_nulls() {8 TestBase testBase = new TestBase();9 testBase.test();10 exception.expect(NullPointerException.class);11 exception.expectMessage("should not allow nulls");12 }13 public void should_not_allow_nulls() {14 TestBase testBase = new TestBase();15 testBase.test();16 exception.expect(NullPointerException.class);17 exception.expectMessage("should not allow nulls");18 }19 public void should_not_allow_nulls() {20 TestBase testBase = new TestBase();21 testBase.test();22 exception.expect(NullPointerException.class);23 exception.expectMessage("should not allow nulls");24 }25 public void should_not_allow_nulls() {26 TestBase testBase = new TestBase();27 testBase.test();28 exception.expect(NullPointerException.class);29 exception.expectMessage("should not allow nulls");30 }31 public void should_not_allow_nulls() {32 TestBase testBase = new TestBase();33 testBase.test();34 exception.expect(NullPointerException.class);35 exception.expectMessage("should not allow nulls");36 }37 public void should_not_allow_nulls() {38 TestBase testBase = new TestBase();
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!