jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject [jmeter] 03/08: Convert to assertThrows
Date Sat, 10 Apr 2021 10:03:39 GMT
This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git

commit 9402524c15eb1b4fae161b6b71798ac1eaba035a
Author: Felix Schumacher <felix.schumacher@internetallee.de>
AuthorDate: Sat Apr 10 11:01:18 2021 +0200

    Convert to assertThrows
    
    Relates to #652
---
 .../org/apache/jorphan/reflect/TestFunctor.java    | 87 ++++------------------
 1 file changed, 16 insertions(+), 71 deletions(-)

diff --git a/src/core/src/test/java/org/apache/jorphan/reflect/TestFunctor.java b/src/core/src/test/java/org/apache/jorphan/reflect/TestFunctor.java
index 83ca1fe..99c525c 100644
--- a/src/core/src/test/java/org/apache/jorphan/reflect/TestFunctor.java
+++ b/src/core/src/test/java/org/apache/jorphan/reflect/TestFunctor.java
@@ -18,7 +18,7 @@
 package org.apache.jorphan.reflect;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.util.Map;
 import java.util.Properties;
@@ -101,20 +101,10 @@ public class TestFunctor extends JMeterTestCase {
         Test2 t2 = new Test2("t2");
         Test1a t1a = new Test1a("aa");
         assertEquals("t1",f1.invoke(t1));
-        try {
-            f1.invoke(t2);
-            fail("Should have generated error");
-        } catch (JMeterError e){
-
-        }
+        assertThrows(JMeterError.class, () -> f1.invoke(t2));
         assertEquals("t2",f2.invoke(t2));
         assertEquals("1a:aa.",f1a.invoke(t1a));
-        try {
-            f1a.invoke(t1);// can't call invoke using super class
-            fail("Should have generated error");
-        } catch (JMeterError e){
-
-        }
+        assertThrows(JMeterError.class, () -> f1a.invoke(t1));
         // OK (currently) to invoke using sub-class
         assertEquals("1a:aa.",f1.invoke(t1a));
     }
@@ -125,17 +115,9 @@ public class TestFunctor extends JMeterTestCase {
         Functor f2 = new Functor("getString");// Args will be provided later
         Test1 t1 = new Test1("t1");
         assertEquals("x1",f.invoke(t1,new String[]{"x1"}));
-        try {
-            assertEquals("x1",f.invoke(t1));
-            fail("Should have generated an Exception");
-        } catch (JMeterError ok){
-        }
+        assertThrows(JMeterError.class, () -> f.invoke(t1));
         assertEquals("x2",f2.invoke(t1,new String[]{"x2"}));
-        try {
-            assertEquals("x2",f2.invoke(t1));
-            fail("Should have generated an Exception");
-        } catch (JMeterError ok){
-        }
+        assertThrows(JMeterError.class, () -> f2.invoke(t1));
     }
 
     @Test
@@ -157,20 +139,10 @@ public class TestFunctor extends JMeterTestCase {
         assertEquals("t1",f1.invoke(t1));
         assertEquals("1a:t1a.",f1.invoke(t1a));
         assertEquals("t2",f1.invoke(t2));
-        try {
-            f1.invoke();
-            fail("Should have failed");
-        } catch (IllegalStateException ok){
-
-        }
+        assertThrows(IllegalStateException.class, () -> f1.invoke());
         Functor f2 = new Functor(HasString.class,"getString");
         assertEquals("xyz",f2.invoke(t2,new String[]{"xyz"}));
-        try {
-            f2.invoke(t1,new String[]{"xyz"});
-            fail("Should have failed");
-        } catch (JMeterError ok){
-
-        }
+        assertThrows(JMeterError.class, () -> f2.invoke(t1,new String[]{"xyz"}));
         Functor f3 = new Functor(t2,"getString");
         assertEquals("xyz",f3.invoke(t2,new Object[]{"xyz"}));
 
@@ -185,46 +157,19 @@ public class TestFunctor extends JMeterTestCase {
 
     @Test
     public void testBadParameters() throws Exception{
-        try {
-            new Functor(null);
-            fail("should have generated IllegalArgumentException;");
-        } catch (IllegalArgumentException ok){}
-        try {
-            new Functor(null,new Class[]{});
-            fail("should have generated IllegalArgumentException;");
-        } catch (IllegalArgumentException ok){}
-        try {
-            new Functor(null,new Object[]{});
-            fail("should have generated IllegalArgumentException;");
-        } catch (IllegalArgumentException ok){}
-        try {
-            new Functor(String.class,null);
-            fail("should have generated IllegalArgumentException;");
-        } catch (IllegalArgumentException ok){}
-        try {
-            new Functor(new Object(),null);
-            fail("should have generated IllegalArgumentException;");
-        } catch (IllegalArgumentException ok){}
-        try {
-            new Functor(new Object(),null, new Class[]{});
-            fail("should have generated IllegalArgumentException;");
-        } catch (IllegalArgumentException ok){}
-        try {
-            new Functor(new Object(),null, new Object[]{});
-            fail("should have generated IllegalArgumentException;");
-        } catch (IllegalArgumentException ok){}
+        assertThrows(IllegalArgumentException.class, () -> new Functor(null));
+        assertThrows(IllegalArgumentException.class, () -> new Functor(null, new Class[]
{}));
+        assertThrows(IllegalArgumentException.class, () -> new Functor(null, new Object[]
{}));
+        assertThrows(IllegalArgumentException.class, () -> new Functor(String.class, null));
+        assertThrows(IllegalArgumentException.class, () -> new Functor(new Object(), null));
+        assertThrows(IllegalArgumentException.class, () -> new Functor(new Object(), null,
new Class[] {}));
+        assertThrows(IllegalArgumentException.class, () -> new Functor(new Object(), null,
new Object[] {}));
     }
 
     @Test
     public void testIllegalState() throws Exception{
         Functor f = new Functor("method");
-        try {
-            f.invoke();
-            fail("should have generated IllegalStateException;");
-        } catch (IllegalStateException ok){}
-        try {
-            f.invoke(new Object[]{});
-            fail("should have generated IllegalStateException;");
-        } catch (IllegalStateException ok){}
+        assertThrows(IllegalStateException.class, ()-> f.invoke());
+        assertThrows(IllegalStateException.class, () -> f.invoke(new Object[]{}));
     }
 }

Mime
View raw message