GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/junit-exception/src/test/java/com/hmkcode/AppTest.java at master · thongh/Java Expand file tree/ Copy pathAppTest.javaMore file actions 59 lines (41 loc) · 1.48 KB / Copy pathAppTest.javaFile metadata and controls59 lines (41 loc) · 1.48 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 package com.hmkcode; import java.util.LinkedList; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import static org.hamcrest.CoreMatchers.is; @RunWith(JUnit4.class) public class AppTest { // ( 1 ) Simple way to test exception @Test(expected= NullPointerException.class) public void testNull() { String str = null; str.toUpperCase(); } // ( 2 ) Test by tyr/catch (test exception message) @Test public void testExceptionMessage(){ try{ new LinkedList<Object>().get(0); //if no exception thrown the test will fail with the below message. fail("Expected an IndexOutOfBoundsException to be thrown"); } catch (IndexOutOfBoundsException anIndexOutOfBoundsException) { //if no exception message is not the same the test will fail with the below message. assertThat(anIndexOutOfBoundsException.getMessage(), is("Index: 0, Size: 0")); } } //( 3 ) Test by @Rule & ExpectedException.expect() | ExpectedException.expectMessage() @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void testExceptionByRule() throws ArithmeticException { thrown.expect(ArithmeticException.class); thrown.expectMessage("/ by zero"); double d = 1 / 0; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |