21
21
import java .util .List ;
22
22
import java .util .Map ;
23
23
24
- import org .junit .Rule ;
25
24
import org .junit .Test ;
26
25
27
- import static org .hamcrest .CoreMatchers .containsString ;
28
- import static org .hamcrest .CoreMatchers .startsWith ;
29
- import static org .junit .Assert .assertNotNull ;
30
- import static org .junit .Assert .assertTrue ;
31
- import static org .junit .Assert .fail ;
32
-
33
26
import org .apache .cassandra .auth .AllowAllAuthorizer ;
34
27
import org .apache .cassandra .auth .IAuthorizer ;
35
28
import org .apache .cassandra .exceptions .ConfigurationException ;
36
- import org .junit .rules .ExpectedException ;
29
+
30
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
31
+ import static org .junit .Assert .assertNotNull ;
32
+ import static org .junit .Assert .assertTrue ;
37
33
38
34
public class ParameterizedClassTest
39
35
{
40
- @ Rule
41
- public ExpectedException exceptionRule = ExpectedException .none ();
42
-
43
36
@ Test
44
37
public void newInstance_NonExistentClass_FailsWithConfigurationException ()
45
38
{
46
- exceptionRule .expect (ConfigurationException .class );
47
-
48
- String expectedError = "Unable to find class NonExistentClass in packages [\" org.apache.cassandra.config\" ]" ;
49
- exceptionRule .expectMessage (expectedError );
50
-
51
- ParameterizedClass parameterizedClass = new ParameterizedClass ("NonExistentClass" );
52
- ParameterizedClass .newInstance (parameterizedClass , List .of ("org.apache.cassandra.config" ));
53
- fail ();
39
+ assertThatThrownBy (() -> ParameterizedClass .newInstance (new ParameterizedClass ("NonExistentClass" ), List .of ("org.apache.cassandra.config" )))
40
+ .hasMessage ("Unable to find class NonExistentClass in packages [\" org.apache.cassandra.config\" ]" )
41
+ .isInstanceOf (ConfigurationException .class );
54
42
}
55
43
56
44
@ Test
@@ -64,14 +52,9 @@ public void newInstance_WithSingleEmptyConstructor_UsesEmptyConstructor()
64
52
@ Test
65
53
public void newInstance_SingleEmptyConstructorWithParameters_FailsWithConfigurationException ()
66
54
{
67
- exceptionRule .expect (ConfigurationException .class );
68
- exceptionRule .expectMessage (startsWith ("No valid constructor found for class" ));
69
-
70
- Map <String , String > parameters = Map .of ("key" , "value" );
71
- ParameterizedClass parameterizedClass = new ParameterizedClass (AllowAllAuthorizer .class .getName (), parameters );
72
-
73
- ParameterizedClass .newInstance (parameterizedClass , null );
74
- fail ();
55
+ assertThatThrownBy (() -> ParameterizedClass .newInstance (new ParameterizedClass (AllowAllAuthorizer .class .getName (), Map .of ("key" , "value" )), null ))
56
+ .hasMessageStartingWith ("No valid constructor found for class" )
57
+ .isInstanceOf (ConfigurationException .class );
75
58
}
76
59
77
60
@ Test
@@ -86,14 +69,10 @@ public void newInstance_WithValidConstructors_FavorsMapConstructor()
86
69
@ Test
87
70
public void newInstance_WithConstructorException_PreservesOriginalFailure ()
88
71
{
89
- exceptionRule .expect (ConfigurationException .class );
90
- exceptionRule .expectMessage (startsWith ("Failed to instantiate class" ));
91
- exceptionRule .expectMessage (containsString ("Simulated failure" ));
92
-
93
- Map <String , String > parameters = Map .of ("fail" , "true" );
94
- ParameterizedClass parameterizedClass = new ParameterizedClass (ParameterizedClassExample .class .getName (), parameters );
95
-
96
- ParameterizedClass .newInstance (parameterizedClass , null );
97
- fail ();
72
+ assertThatThrownBy (() -> ParameterizedClass .newInstance (new ParameterizedClass (ParameterizedClassExample .class .getName (),
73
+ Map .of ("fail" , "true" )), null ))
74
+ .hasMessageContaining ("Simulated failure" )
75
+ .hasMessageStartingWith ("Failed to instantiate class" )
76
+ .isInstanceOf (ConfigurationException .class );
98
77
}
99
78
}
0 commit comments