Best junit code snippet using org.junit.rules.ExpectedException.handleAssumptionViolatedExceptions
org.junit.rules.ExpectedException
The ExpectedException is a rule which helps scripts to verify that the script throws the specific exception or not
Here are code snippets that can help you understand more how developers are using
Source:ExpectedException.java
...16public class ExpectedException17implements TestRule {18 private final ExpectedExceptionMatcherBuilder fMatcherBuilder = new ExpectedExceptionMatcherBuilder();19 private boolean handleAssertionErrors = false;20 private boolean handleAssumptionViolatedExceptions = false;21 private ExpectedException() {22 }23 private void failDueToMissingException() throws AssertionError {24 String string2 = StringDescription.toString(this.fMatcherBuilder.build());25 Assert.fail("Expected test to throw " + string2);26 }27 private void handleException(Throwable throwable) throws Throwable {28 if (this.fMatcherBuilder.expectsThrowable()) {29 Assert.assertThat(throwable, this.fMatcherBuilder.build());30 return;31 }32 throw throwable;33 }34 public static ExpectedException none() {35 return new ExpectedException();36 }37 private void optionallyHandleException(Throwable throwable, boolean bl2) throws Throwable {38 if (bl2) {39 this.handleException(throwable);40 return;41 }42 throw throwable;43 }44 @Override45 public Statement apply(Statement statement, Description description) {46 return new ExpectedExceptionStatement(statement);47 }48 public void expect(Class<? extends Throwable> class_) {49 this.expect(CoreMatchers.instanceOf(class_));50 }51 public void expect(Matcher<?> matcher) {52 this.fMatcherBuilder.add(matcher);53 }54 public void expectCause(Matcher<? extends Throwable> matcher) {55 this.expect(ThrowableCauseMatcher.hasCause(matcher));56 }57 public void expectMessage(String string2) {58 this.expectMessage(CoreMatchers.containsString(string2));59 }60 public void expectMessage(Matcher<String> matcher) {61 this.expect(ThrowableMessageMatcher.hasMessage(matcher));62 }63 public ExpectedException handleAssertionErrors() {64 this.handleAssertionErrors = true;65 return this;66 }67 public ExpectedException handleAssumptionViolatedExceptions() {68 this.handleAssumptionViolatedExceptions = true;69 return this;70 }71 private class ExpectedExceptionStatement72 extends Statement {73 private final Statement fNext;74 public ExpectedExceptionStatement(Statement statement) {75 this.fNext = statement;76 }77 @Override78 public void evaluate() throws Throwable {79 try {80 this.fNext.evaluate();81 if (ExpectedException.this.fMatcherBuilder.expectsThrowable()) {82 ExpectedException.this.failDueToMissingException();83 }84 return;85 }86 catch (AssumptionViolatedException var1_1) {87 ExpectedException.this.optionallyHandleException(var1_1, ExpectedException.this.handleAssumptionViolatedExceptions);88 return;89 }90 catch (AssertionError var1_2) {91 ExpectedException.this.optionallyHandleException((Throwable)((Object)var1_2), ExpectedException.this.handleAssertionErrors);92 return;93 }94 catch (Throwable var1_3) {95 ExpectedException.this.handleException(var1_3);96 return;97 }98 }99 }100}...
handleAssumptionViolatedExceptions
Using AI Code Generation
1public ExpectedException thrown = ExpectedException.none();2public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {3 List<Object> list = new ArrayList<Object>();4 thrown.expect(IndexOutOfBoundsException.class);5 thrown.expectMessage("Index: 0, Size: 0");6 thrown.expectMessage(startsWith("Index:"));7 thrown.expectMessage(endsWith("Size: 0"));8 list.get(0);9}10 at org.junit.Assert.assertEquals(Assert.java:115)11 at org.junit.Assert.assertEquals(Assert.java:144)12 at com.baeldung.junit.exception.ExpectedExceptionTest.shouldTestExceptionMessage(ExpectedExceptionTest.java:19)13public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {14 List<Object> list = new ArrayList<Object>();15 thrown.expect(AssertionError.class);16 thrown.expectMessage("Index: 0, Size: 0");17 thrown.expectMessage(startsWith("Index:"));18 thrown.expectMessage(endsWith("Size: 0"));19 Assert.assertEquals("Index: 0, Size: 0", list.get(0));20}21 at org.junit.Assert.assertEquals(Assert.java:115)22 at org.junit.Assert.assertEquals(Assert.java:144)23 at com.baeldung.junit.exception.ExpectedExceptionTest.shouldTestExceptionMessage(ExpectedExceptionTest.java:19)24public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {25 List<Object> list = new ArrayList<Object>();26 thrown.expect(AssertionError.class);27 thrown.expectMessage("Index: 0, Size: 0");28 thrown.expectMessage(startsWith("Index:"));29 thrown.expectMessage(endsWith("Size: 0"));30 Assume.assumeTrue(list.get(0).equals("Index: 0, Size: 0"));31}32 at org.junit.Assume.assumeThat(Assume.java:95)
handleAssumptionViolatedExceptions
Using AI Code Generation
1@Rule public ExpectedException thrown = ExpectedException.none();2@Rule public ExpectedException thrown = ExpectedException.none();3@Rule public ExpectedException thrown = ExpectedException.none();4@Rule public ExpectedException thrown = ExpectedException.none();5@Rule public ExpectedException thrown = ExpectedException.none();6@Rule public ExpectedException thrown = ExpectedException.none();7@Rule public ExpectedException thrown = ExpectedException.none();8@Rule public ExpectedException thrown = ExpectedException.none();9@Rule public ExpectedException thrown = ExpectedException.none();10@Rule public ExpectedException thrown = ExpectedException.none();11@Rule public ExpectedException thrown = ExpectedException.none();12@Rule public ExpectedException thrown = ExpectedException.none();13@Rule public ExpectedException thrown = ExpectedException.none();14@Rule public ExpectedException thrown = ExpectedException.none();15@Rule public ExpectedException thrown = ExpectedException.none();16@Rule public ExpectedException thrown = ExpectedException.none();
handleAssumptionViolatedExceptions
Using AI Code Generation
1import org.junit.*;2import org.junit.rules.*;3import static org.junit.Assert.*;4import static org.junit.rules.ExpectedException.*;5public class TestException {6 public ExpectedException thrown = none();7 public void throwsNothing() {8 }9 public void throwsNullPointerException() {10 thrown.expect(NullPointerException.class);11 throw new NullPointerException();12 }13 public void throwsNullPointerExceptionWithMessage() {14 thrown.expect(NullPointerException.class);15 thrown.expectMessage("happened");16 throw new NullPointerException("What happened?");17 }18 public void throwsNullPointerExceptionWithMessageUsingMatcher() {19 thrown.expect(NullPointerException.class);20 thrown.expectMessage(containsString("happened"));21 throw new NullPointerException("What happened?");22 }23}
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!