Best Testng code snippet using org.testng.Assert.assertThrows
Source:EmptyNavigableSet.java
...62 }63 public interface Thrower<T extends Throwable> {64 public void run() throws T;65 }66 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {67 assertThrows(thrower, throwable, null);68 }69 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {70 Throwable result;71 try {72 thrower.run();73 fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");74 return;75 } catch (Throwable caught) {76 result = caught;77 }78 assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");79 }80 public static final boolean isDescending(SortedSet<?> set) {81 if (null == set.comparator()) {82 // natural order83 return false;84 }85 if (Collections.reverseOrder() == set.comparator()) {86 // reverse natural order.87 return true;88 }89 if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {90 // it's a Collections.reverseOrder(Comparator).91 return true;92 }93 throw new IllegalStateException("can't determine ordering for " + set);94 }95 /**96 * Tests that the comparator is {@code null}.97 */98 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)99 public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {100 Comparator comparator = navigableSet.comparator();101 assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");102 }103 /**104 * Tests that contains requires Comparable105 */106 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)107 public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {108 assertThrows(() -> {109 navigableSet.contains(new Object());110 },111 ClassCastException.class,112 description + ": Compareable should be required");113 }114 /**115 * Tests that the contains method returns {@code false}.116 */117 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)118 public void testContains(String description, NavigableSet<?> navigableSet) {119 assertFalse(navigableSet.contains(new Integer(1)),120 description + ": Should not contain any elements.");121 }122 /**123 * Tests that the containsAll method returns {@code false}.124 */125 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)126 public void testContainsAll(String description, NavigableSet<?> navigableSet) {127 TreeSet treeSet = new TreeSet();128 treeSet.add("1");129 treeSet.add("2");130 treeSet.add("3");131 assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements.");132 }133 /**134 * Tests that the iterator is empty.135 */136 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)137 public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {138 Iterator emptyIterator = navigableSet.iterator();139 assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),140 "The iterator is not empty.");141 }142 /**143 * Tests that the set is empty.144 */145 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)146 public void testIsEmpty(String description, NavigableSet<?> navigableSet) {147 assertTrue(navigableSet.isEmpty(), "The set is not empty.");148 }149 /**150 * Tests that the first() method throws NoSuchElementException151 */152 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)153 public void testFirst(String description, NavigableSet<?> navigableSet) {154 assertThrows(() -> {155 navigableSet.first();156 }, NoSuchElementException.class, description);157 }158 /**159 * Tests the headSet() method.160 */161 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)162 public void testHeadSet(String description, NavigableSet navigableSet) {163 assertThrows(164 () -> { NavigableSet ns = navigableSet.headSet(null, false); },165 NullPointerException.class,166 description + ": Must throw NullPointerException for null element");167 assertThrows(168 () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },169 ClassCastException.class,170 description + ": Must throw ClassCastException for non-Comparable element");171 NavigableSet ns = navigableSet.headSet("1", false);172 assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set.");173 }174 /**175 * Tests that the last() method throws NoSuchElementException176 */177 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)178 public void testLast(String description, NavigableSet<?> navigableSet) {179 assertThrows(() -> {180 navigableSet.last();181 }, NoSuchElementException.class, description);182 }183 /**184 * Tests that the size is 0.185 */186 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)187 public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {188 assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");189 }190 /**191 * Tests the subSet() method.192 */193 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)194 public void testSubSet(String description, NavigableSet navigableSet) {195 assertThrows(196 () -> {197 SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);198 },199 NullPointerException.class,200 description + ": Must throw NullPointerException for null element");201 assertThrows(202 () -> {203 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);204 },205 NullPointerException.class,206 description + ": Must throw NullPointerException for null element");207 assertThrows(208 () -> {209 SortedSet ss = navigableSet.subSet(null, null);210 },211 NullPointerException.class,212 description + ": Must throw NullPointerException for null element");213 Object obj1 = new Object();214 Object obj2 = new Object();215 assertThrows(216 () -> {217 SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);218 },219 ClassCastException.class, description220 + ": Must throw ClassCastException for parameter which is not Comparable.");221 assertThrows(222 () -> {223 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);224 },225 ClassCastException.class, description226 + ": Must throw ClassCastException for parameter which is not Comparable.");227 assertThrows(228 () -> {229 SortedSet ss = navigableSet.subSet(obj1, obj2);230 },231 ClassCastException.class, description232 + ": Must throw ClassCastException for parameter which is not Comparable.");233 // minimal range234 navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);235 navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);236 navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);237 navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);238 Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;239 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;240 assertThrows(241 () -> {242 navigableSet.subSet(last, true, first, false);243 },244 IllegalArgumentException.class, description245 + ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");246 navigableSet.subSet(first, true, last, false);247 }248 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)249 public void testSubSetRanges(String description, NavigableSet navigableSet) {250 Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;251 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;252 NavigableSet subSet = navigableSet.subSet(first, true, last, true);253 // same subset254 subSet.subSet(first, true, last, true);255 // slightly smaller256 NavigableSet ns = subSet.subSet(first, false, last, false);257 // slight exapansion258 assertThrows(() -> {259 ns.subSet(first, true, last, true);260 },261 IllegalArgumentException.class,262 description + ": Expansion should not be allowed");263 // much smaller264 subSet.subSet(first, false, BigInteger.ONE, false);265 }266 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)267 public void testheadSetRanges(String description, NavigableSet navigableSet) {268 NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);269 // same subset270 subSet.headSet(BigInteger.ONE, true);271 // slightly smaller272 NavigableSet ns = subSet.headSet(BigInteger.ONE, false);273 // slight exapansion274 assertThrows(() -> {275 ns.headSet(BigInteger.ONE, true);276 },277 IllegalArgumentException.class,278 description + ": Expansion should not be allowed");279 // much smaller280 subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);281 }282 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)283 public void testTailSetRanges(String description, NavigableSet navigableSet) {284 NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);285 // same subset286 subSet.tailSet(BigInteger.ONE, true);287 // slightly smaller288 NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);289 // slight exapansion290 assertThrows(() -> {291 ns.tailSet(BigInteger.ONE, true);292 },293 IllegalArgumentException.class,294 description + ": Expansion should not be allowed");295 // much smaller296 subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);297 }298 /**299 * Tests the tailSet() method.300 */301 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)302 public void testTailSet(String description, NavigableSet navigableSet) {303 assertThrows(() -> {304 navigableSet.tailSet(null);305 },306 NullPointerException.class,307 description + ": Must throw NullPointerException for null element");308 assertThrows(() -> {309 navigableSet.tailSet(new Object());310 }, ClassCastException.class);311 NavigableSet ss = navigableSet.tailSet("1", true);312 assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");313 }314 /**315 * Tests that the array has a size of 0.316 */317 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)318 public void testToArray(String description, NavigableSet<?> navigableSet) {319 Object[] emptyNavigableSetArray = navigableSet.toArray();320 assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");321 emptyNavigableSetArray = new Object[20];322 Object[] result = navigableSet.toArray(emptyNavigableSetArray);...
Source:EmptyNavigableMap.java
...62 }63 public interface Thrower<T extends Throwable> {64 public void run() throws T;65 }66 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {67 assertThrows(thrower, throwable, null);68 }69 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {70 Throwable result;71 try {72 thrower.run();73 fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");74 return;75 } catch (Throwable caught) {76 result = caught;77 }78 assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");79 }80 public static final boolean isDescending(SortedMap<?,?> set) {81 if (null == set.comparator()) {82 // natural order83 return false;84 }85 if (Collections.reverseOrder() == set.comparator()) {86 // reverse natural order.87 return true;88 }89 if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {90 // it's a Collections.reverseOrder(Comparator).91 return true;92 }93 throw new IllegalStateException("can't determine ordering for " + set);94 }95 /**96 * Tests that the comparator is {@code null}.97 */98 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)99 public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) {100 Comparator comparator = navigableMap.comparator();101 assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");102 }103 /**104 * Tests that contains requires Comparable105 */106 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)107 public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {108 assertThrows(() -> {109 navigableMap.containsKey(new Object());110 },111 ClassCastException.class,112 description + ": Compareable should be required");113 }114 /**115 * Tests that the contains method returns {@code false}.116 */117 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)118 public void testContains(String description, NavigableMap<?,?> navigableMap) {119 assertFalse(navigableMap.containsKey(new Integer(1)),120 description + ": Should not contain any elements.");121 assertFalse(navigableMap.containsValue(new Integer(1)),122 description + ": Should not contain any elements.");123 }124 /**125 * Tests that the containsAll method returns {@code false}.126 */127 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)128 public void testContainsAll(String description, NavigableMap<?,?> navigableMap) {129 TreeMap treeMap = new TreeMap();130 treeMap.put("1", 1);131 treeMap.put("2", 2);132 treeMap.put("3", 3);133 assertFalse(navigableMap.equals(treeMap), "Should not contain any elements.");134 }135 /**136 * Tests that the iterator is empty.137 */138 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)139 public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) {140 assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty.");141 assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty.");142 assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty.");143 }144 /**145 * Tests that the set is empty.146 */147 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)148 public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) {149 assertTrue(navigableMap.isEmpty(), "The set is not empty.");150 }151 /**152 * Tests the headMap() method.153 */154 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)155 public void testHeadMap(String description, NavigableMap navigableMap) {156 assertThrows(157 () -> { NavigableMap ss = navigableMap.headMap(null, false); },158 NullPointerException.class,159 description + ": Must throw NullPointerException for null element");160 assertThrows(161 () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },162 ClassCastException.class,163 description + ": Must throw ClassCastException for non-Comparable element");164 NavigableMap ss = navigableMap.headMap("1", false);165 assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");166 }167 /**168 * Tests that the size is 0.169 */170 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)171 public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) {172 assertTrue(0 == navigableMap.size(), "The size of the set is not 0.");173 }174 /**175 * Tests the subMap() method.176 */177 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)178 public void testSubMap(String description, NavigableMap navigableMap) {179 assertThrows(180 () -> {181 SortedMap ss = navigableMap.subMap(null, BigInteger.TEN);182 },183 NullPointerException.class,184 description + ": Must throw NullPointerException for null element");185 assertThrows(186 () -> {187 SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null);188 },189 NullPointerException.class,190 description + ": Must throw NullPointerException for null element");191 assertThrows(192 () -> {193 SortedMap ss = navigableMap.subMap(null, null);194 },195 NullPointerException.class,196 description + ": Must throw NullPointerException for null element");197 Object obj1 = new Object();198 Object obj2 = new Object();199 assertThrows(200 () -> {201 SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN);202 },203 ClassCastException.class, description204 + ": Must throw ClassCastException for parameter which is not Comparable.");205 assertThrows(206 () -> {207 SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2);208 },209 ClassCastException.class, description210 + ": Must throw ClassCastException for parameter which is not Comparable.");211 assertThrows(212 () -> {213 SortedMap ss = navigableMap.subMap(obj1, obj2);214 },215 ClassCastException.class, description216 + ": Must throw ClassCastException for parameter which is not Comparable.");217 // minimal range218 navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false);219 navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true);220 navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false);221 navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true);222 Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;223 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;224 assertThrows(225 () -> {226 navigableMap.subMap(last, true, first, false);227 },228 IllegalArgumentException.class, description229 + ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");230 navigableMap.subMap(first, true, last, false);231 }232 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)233 public void testSubMapRanges(String description, NavigableMap navigableMap) {234 Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;235 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;236 NavigableMap subMap = navigableMap.subMap(first, true, last, true);237 // same subset238 subMap.subMap(first, true, last, true);239 // slightly smaller240 NavigableMap ns = subMap.subMap(first, false, last, false);241 // slight exapansion242 assertThrows(() -> {243 ns.subMap(first, true, last, true);244 },245 IllegalArgumentException.class,246 description + ": Expansion should not be allowed");247 // much smaller248 subMap.subMap(first, false, BigInteger.ONE, false);249 }250 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)251 public void testheadMapRanges(String description, NavigableMap navigableMap) {252 NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);253 // same subset254 subMap.headMap(BigInteger.ONE, true);255 // slightly smaller256 NavigableMap ns = subMap.headMap(BigInteger.ONE, false);257 // slight exapansion258 assertThrows(() -> {259 ns.headMap(BigInteger.ONE, true);260 },261 IllegalArgumentException.class,262 description + ": Expansion should not be allowed");263 // much smaller264 subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);265 }266 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)267 public void testTailMapRanges(String description, NavigableMap navigableMap) {268 NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);269 // same subset270 subMap.tailMap(BigInteger.ONE, true);271 // slightly smaller272 NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);273 // slight exapansion274 assertThrows(() -> {275 ns.tailMap(BigInteger.ONE, true);276 },277 IllegalArgumentException.class,278 description + ": Expansion should not be allowed");279 // much smaller280 subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);281 }282 /**283 * Tests the tailMap() method.284 */285 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)286 public void testTailMap(String description, NavigableMap navigableMap) {287 assertThrows(() -> {288 navigableMap.tailMap(null);289 },290 NullPointerException.class,291 description + ": Must throw NullPointerException for null element");292 assertThrows(() -> {293 navigableMap.tailMap(new Object());294 }, ClassCastException.class);295 NavigableMap ss = navigableMap.tailMap("1", true);296 assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");297 }298 @DataProvider(name = "NavigableMap<?,?>", parallel = true)299 public static Iterator<Object[]> navigableMapsProvider() {300 return makeNavigableMaps().iterator();301 }302 public static Collection<Object[]> makeNavigableMaps() {303 return Arrays.asList(304 new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())},305 new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())},306 new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())},...
Source:TestKitTest.java
...45 Integer integer = TestKit.assertNotThrows(46 () -> TestKit.assertNotThrows(() -> 1)47 );48 assertEquals(integer, Integer.valueOf(1));49 RuntimeException re = TestKit.assertThrows(50 RuntimeException.class,51 () -> TestKit.assertNotThrows(() -> { throw new IOException(); })52 );53 assertEquals(re.getMessage(),54 "Expected to run normally, but threw "55 + "java.io.IOException");56 TestKit.assertNotThrows(57 () -> TestKit.assertNotThrows(() -> { })58 );59 re = TestKit.assertThrows(60 RuntimeException.class,61 () -> TestKit.assertNotThrows((TestKit.ThrowingProcedure) () -> { throw new IOException(); })62 );63 assertEquals(re.getMessage(),64 "Expected to run normally, but threw "65 + "java.io.IOException");66 }67 @Test68 public void testAssertThrows() {69 NullPointerException npe = TestKit.assertThrows(70 NullPointerException.class,71 () -> TestKit.assertThrows(null, null)72 );73 assertNotNull(npe);74 assertTrue(Set.of("clazz", "code").contains(npe.getMessage()), npe.getMessage());75 npe = TestKit.assertThrows(76 NullPointerException.class,77 () -> TestKit.assertThrows(IOException.class, null)78 );79 assertNotNull(npe);80 assertEquals(npe.getMessage(), "code");81 npe = TestKit.assertThrows(82 NullPointerException.class,83 () -> TestKit.assertThrows(null, () -> { })84 );85 assertEquals(npe.getMessage(), "clazz");86 npe = TestKit.assertThrows(87 NullPointerException.class,88 () -> { throw new NullPointerException(); }89 );90 assertNotNull(npe);91 assertNull(npe.getMessage());92 assertEquals(npe.getClass(), NullPointerException.class);93 RuntimeException re = TestKit.assertThrows(94 RuntimeException.class,95 () -> TestKit.assertThrows(NullPointerException.class, () -> { })96 );97 assertEquals(re.getClass(), RuntimeException.class);98 assertEquals(re.getMessage(),99 "Expected to catch an exception of type "100 + "java.lang.NullPointerException, but caught nothing");101 re = TestKit.assertThrows(102 RuntimeException.class,103 () -> { throw new NullPointerException(); }104 );105 assertNotNull(re);106 assertNull(re.getMessage());107 assertEquals(re.getClass(), NullPointerException.class);108 re = TestKit.assertThrows(109 RuntimeException.class,110 () -> TestKit.assertThrows(111 IllegalFormatException.class,112 () -> { throw new IndexOutOfBoundsException(); }113 ));114 assertNotNull(re);115 assertEquals(re.getClass(), RuntimeException.class);116 assertEquals(re.getMessage(),117 "Expected to catch an exception of type java.util.IllegalFormatException"118 + ", but caught java.lang.IndexOutOfBoundsException");119 }120 @Test121 public void testAssertUnmodifiable() {122 TestKit.assertUnmodifiableList(123 Collections.unmodifiableList(124 new ArrayList<>(Arrays.asList(1, 2, 3))));125 TestKit.assertThrows(RuntimeException.class,126 () -> TestKit.assertUnmodifiableList(new ArrayList<>()));127 TestKit.assertThrows(RuntimeException.class,128 () -> TestKit.assertUnmodifiableList(new LinkedList<>()));129 TestKit.assertThrows(RuntimeException.class,130 () -> TestKit.assertUnmodifiableList(131 new ArrayList<>(Arrays.asList(1, 2, 3))));132 TestKit.assertUnmodifiableMap(Collections.unmodifiableMap(Map.of()));133 TestKit.assertThrows(RuntimeException.class,134 () -> TestKit.assertUnmodifiableMap(new HashMap<>()));135 }136}...
Source:SendTest.java
...34import static java.net.http.HttpClient.Builder.NO_PROXY;35import static java.net.http.HttpClient.newBuilder;36import static java.net.http.WebSocket.NORMAL_CLOSURE;37import static org.testng.Assert.assertEquals;38import static org.testng.Assert.assertThrows;39import static org.testng.Assert.assertTrue;40public class SendTest {41 private static final Class<NullPointerException> NPE = NullPointerException.class;42 private DummyWebSocketServer server;43 private WebSocket webSocket;44 @AfterTest45 public void cleanup() {46 server.close();47 webSocket.abort();48 }49 @Test50 public void sendMethodsThrowNPE() throws IOException {51 server = new DummyWebSocketServer();52 server.open();53 webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()54 .buildAsync(server.getURI(), new WebSocket.Listener() { })55 .join();56 assertThrows(NPE, () -> webSocket.sendText(null, false));57 assertThrows(NPE, () -> webSocket.sendText(null, true));58 assertThrows(NPE, () -> webSocket.sendBinary(null, false));59 assertThrows(NPE, () -> webSocket.sendBinary(null, true));60 assertThrows(NPE, () -> webSocket.sendPing(null));61 assertThrows(NPE, () -> webSocket.sendPong(null));62 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));63 webSocket.abort();64 assertThrows(NPE, () -> webSocket.sendText(null, false));65 assertThrows(NPE, () -> webSocket.sendText(null, true));66 assertThrows(NPE, () -> webSocket.sendBinary(null, false));67 assertThrows(NPE, () -> webSocket.sendBinary(null, true));68 assertThrows(NPE, () -> webSocket.sendPing(null));69 assertThrows(NPE, () -> webSocket.sendPong(null));70 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));71 }72 // TODO: request in onClose/onError73 // TODO: throw exception in onClose/onError74 // TODO: exception is thrown from request()75 @Test76 public void sendCloseCompleted() throws IOException {77 server = new DummyWebSocketServer();78 server.open();79 webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()80 .buildAsync(server.getURI(), new WebSocket.Listener() { })81 .join();82 webSocket.sendClose(NORMAL_CLOSURE, "").join();83 assertTrue(webSocket.isOutputClosed());84 assertEquals(webSocket.getSubprotocol(), "");...
Source:Class4_4Test.java
...5public class Class4_4Test {6 @Test7 public void getIntOfString() {8 System.out.println(Class4_4.getIntOfString("10", "5"));9 Assert.assertThrows(ArithmeticException.class,10 () -> Class4_4.getIntOfString("10", "0"));11 Assert.assertThrows(NumberFormatException.class,12 () -> Class4_4.getIntOfString("r", "1"));13 Assert.assertThrows(NumberFormatException.class,14 () -> Class4_4.getIntOfString("r", "a"));15 }16}...
assertThrows
Using AI Code Generation
1import org.testng.Assert;2import org.testng.annotations.Test;3public class TestNGAssertThrows {4 public void testAssertThrows() {5 Assert.assertThrows(IllegalArgumentException.class, () -> {6 throw new IllegalArgumentException("Expected exception");7 });8 }9}10Method testAssertThrows() should have thrown an exception of type class java.lang.IllegalArgumentException11at org.testng.Assert.assertThrows(Assert.java:1062)12at org.testng.Assert.assertThrows(Assert.java:1039)13at com.baeldung.testng.AssertThrowsTest.testAssertThrows(AssertThrowsTest.java:10)14at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)15at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)16at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)17at java.lang.reflect.Method.invoke(Method.java:498)18at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)19at org.testng.internal.Invoker.invokeMethod(Invoker.java:599)20at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:174)21at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:146)22at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)23at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)24at org.testng.TestRunner.privateRun(TestRunner.java:764)25at org.testng.TestRunner.run(TestRunner.java:585)26at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)27at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)28at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)29at org.testng.SuiteRunner.run(SuiteRunner.java:286)30at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)31at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)32at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)33at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)34at org.testng.TestNG.run(TestNG.java:1049)35at org.testng.TestNG.privateMain(TestNG.java:1354)36at org.testng.TestNG.main(TestNG.java:1323)37Assert.assertThrows()
assertThrows
Using AI Code Generation
1import org.testng.Assert;2import org.testng.annotations.Test;3import org.testng.asserts.SoftAssert;4public class TestNGSoftAssert {5 public void testSoftAssert() {6 SoftAssert softAssert = new SoftAssert();7 softAssert.assertEquals(1, 2, "1 is not equal to 2");8 softAssert.assertTrue(false, "This is false");9 softAssert.assertAll();10 }11 public void testSoftAssertWithAssertThrows() {12 SoftAssert softAssert = new SoftAssert();13 softAssert.assertThrows(AssertionError.class, () -> {14 Assert.assertEquals(1, 2, "1 is not equal to 2");15 });16 softAssert.assertThrows(AssertionError.class, () -> {17 Assert.assertTrue(false, "This is false");18 });19 softAssert.assertAll();20 }21}22 at org.testng.Assert.fail(Assert.java:94)23 at org.testng.Assert.failNotEquals(Assert.java:494)24 at org.testng.Assert.assertEquals(Assert.java:123)25 at org.testng.Assert.assertEquals(Assert.java:370)26 at org.testng.Assert.assertEquals(Assert.java:380)27 at TestNGSoftAssert.testSoftAssert(TestNGSoftAssert.java:14)28 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)29 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)30 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)31 at java.lang.reflect.Method.invoke(Method.java:498)32 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)33 at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)34 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)35 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)36 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)37 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)38 at org.testng.TestRunner.privateRun(TestRunner.java:648)39 at org.testng.TestRunner.run(TestRunner.java:505)40 at org.testng.SuiteRunner.runTest(S
assertThrows
Using AI Code Generation
1public void testAssertThrows() {2 assertThrows(IllegalArgumentException.class, () -> {3 throw new IllegalArgumentException("Not a valid argument");4 });5}6public void testAssertThrows() {7 assertThrows(IllegalArgumentException.class, () -> {8 throw new IllegalArgumentException("Not a valid argument");9 }, "Message to print if the assertion fails");10}11public void testAssertThrows() {12 assertThrows(IllegalArgumentException.class, () -> {13 throw new IllegalArgumentException("Not a valid argument");14 }, "Message to print if the assertion fails", "Expected value");15}16public void testAssertThrows() {17 assertThrows(IllegalArgumentException.class, () -> {18 throw new IllegalArgumentException("Not a valid argument");19 }, "Message to print if the assertion fails", "Expected value", "Actual value");20}21public void testAssertThrows() {22 assertThrows(IllegalArgumentException.class, () -> {23 throw new IllegalArgumentException("Not a valid argument");24 }, "Message to print if the assertion fails", "Expected value", "Actual value", "Delta value");25}26public void testAssertThrows() {27 assertThrows(IllegalArgumentException.class, () -> {28 throw new IllegalArgumentException("Not a valid argument");29 }, "Message to print if the assertion fails", "Expected value", "Actual value", "Delta value", "Expected object");30}31public void testAssertThrows() {32 assertThrows(IllegalArgumentException.class, () -> {33 throw new IllegalArgumentException("Not a valid argument");34 }, "Message to print if the assertion fails", "Expected value", "Actual value", "Delta value", "Expected object", "Actual object");35}36public void testAssertThrows() {37 assertThrows(IllegalArgumentException.class, () -> {38 throw new IllegalArgumentException("Not a valid argument");39 }, "Message to print if the assertion fails", "Expected value", "Actual value", "Delta value", "Expected object", "Actual object", "Expected array", "Actual array");40}
assertThrows
Using AI Code Generation
1import org.testng.Assert;2import org.testng.annotations.Test;3public class TestNG_AssertThrows {4 public void testAssertThrows() {5 Assert.assertThrows(IllegalArgumentException.class, () -> {6 throw new IllegalArgumentException("IllegalArgumentException");7 });8 }9}10Method testAssertThrows() of class TestNG_AssertThrows threw an exception: java.lang.IllegalArgumentException: IllegalArgumentException11 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)12 at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)13 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)14 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)15 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)16 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)17 at org.testng.TestRunner.privateRun(TestRunner.java:767)18 at org.testng.TestRunner.run(TestRunner.java:617)19 at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)20 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)21 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)22 at org.testng.SuiteRunner.run(SuiteRunner.java:254)23 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)24 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)25 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)26 at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)27 at org.testng.TestNG.runSuites(TestNG.java:1029)28 at org.testng.TestNG.run(TestNG.java:996)29 at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)30 at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)31 at TestNG_AssertThrows.testAssertThrows(TestNG_AssertThrows.java:15)32 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)33 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
assertThrows
Using AI Code Generation
1public void test() throws Exception {2 assertThrows(NullPointerException.class, () -> {3 throw new NullPointerException();4 });5}6public void test() throws Exception {7 assertThrows(NullPointerException.class, () -> {8 throw new NullPointerException();9 });10}11public void test() throws Exception {12 assertThrows(NullPointerException.class, () -> {13 throw new NullPointerException();14 });15}16public void test() throws Exception {17 assertThrows(NullPointerException.class, () -> {18 throw new NullPointerException();19 });20}21public void test() throws Exception {22 assertThrows(NullPointerException.class, () -> {23 throw new NullPointerException();24 });25}26public void test() throws Exception {27 assertThrows(NullPointerException.class, () -> {28 throw new NullPointerException();29 });30}31public void test() throws Exception {32 assertThrows(NullPointerException.class, () -> {33 throw new NullPointerException();34 });35}36public void test() throws Exception {37 assertThrows(NullPointerException.class, () -> {38 throw new NullPointerException();39 });40}41public void test() throws Exception {42 assertThrows(NullPointerException.class, () -> {43 throw new NullPointerException();44 });45}46public void test() throws Exception {47 assertThrows(NullPointerException.class, () -> {48 throw new NullPointerException();49 });50}
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!