16
16
17
17
package io .grpc ;
18
18
19
+ import static com .google .common .truth .Truth .assertThat ;
20
+ import static org .junit .Assert .assertThrows ;
19
21
import static org .junit .Assert .assertTrue ;
20
22
23
+ import com .google .common .truth .StringSubject ;
21
24
import io .grpc .MethodDescriptor .MethodType ;
22
25
import io .grpc .testing .TestMethodDescriptors ;
23
26
import java .util .Arrays ;
24
27
import java .util .Collection ;
25
28
import java .util .Collections ;
26
29
import java .util .List ;
27
- import org .junit .Rule ;
28
30
import org .junit .Test ;
29
- import org .junit .rules .ExpectedException ;
30
31
import org .junit .runner .RunWith ;
31
32
import org .junit .runners .JUnit4 ;
32
33
36
37
@ RunWith (JUnit4 .class )
37
38
public class ServiceDescriptorTest {
38
39
39
- @ SuppressWarnings ("deprecation" ) // https://github.com/grpc/grpc-java/issues/7467
40
- @ Rule
41
- public final ExpectedException thrown = ExpectedException .none ();
42
-
43
40
@ Test
44
41
public void failsOnNullName () {
45
- thrown . expect ( NullPointerException . class );
46
- thrown . expectMessage ( "name" );
47
-
48
- new ServiceDescriptor ( null , Collections .< MethodDescriptor <?, ?>> emptyList () );
42
+ List < MethodDescriptor <?, ?>> methods = Collections . emptyList ( );
43
+ NullPointerException e = assertThrows ( NullPointerException . class ,
44
+ () -> new ServiceDescriptor ( null , methods ));
45
+ assertThat ( e ). hasMessageThat (). isEqualTo ( "name" );
49
46
}
50
47
51
48
@ Test
52
49
public void failsOnNullMethods () {
53
- thrown .expect (NullPointerException .class );
54
- thrown .expectMessage ("methods" );
55
-
56
- new ServiceDescriptor ("name" , (Collection <MethodDescriptor <?, ?>>) null );
50
+ NullPointerException e = assertThrows (NullPointerException .class ,
51
+ () -> new ServiceDescriptor ("name" , (Collection <MethodDescriptor <?, ?>>) null ));
52
+ assertThat (e ).hasMessageThat ().isEqualTo ("methods" );
57
53
}
58
54
59
55
@ Test
60
56
public void failsOnNullMethod () {
61
- thrown . expect ( NullPointerException . class );
62
- thrown . expectMessage ( "method" );
63
-
64
- new ServiceDescriptor ( "name" , Collections .< MethodDescriptor <?, ?>> singletonList ( null ) );
57
+ List < MethodDescriptor <?, ?>> methods = Collections . singletonList ( null );
58
+ NullPointerException e = assertThrows ( NullPointerException . class ,
59
+ () -> new ServiceDescriptor ( "name" , methods ));
60
+ assertThat ( e ). hasMessageThat (). isEqualTo ( "method" );
65
61
}
66
62
67
63
@ Test
68
64
public void failsOnNonMatchingNames () {
69
65
List <MethodDescriptor <?, ?>> descriptors = Collections .<MethodDescriptor <?, ?>>singletonList (
70
66
MethodDescriptor .<Void , Void >newBuilder ()
71
67
.setType (MethodType .UNARY )
72
- .setFullMethodName (MethodDescriptor .generateFullMethodName ("wrongservice " , "method" ))
68
+ .setFullMethodName (MethodDescriptor .generateFullMethodName ("wrongService " , "method" ))
73
69
.setRequestMarshaller (TestMethodDescriptors .voidMarshaller ())
74
70
.setResponseMarshaller (TestMethodDescriptors .voidMarshaller ())
75
71
.build ());
76
72
77
- thrown .expect (IllegalArgumentException .class );
78
- thrown .expectMessage ("service names" );
79
-
80
- new ServiceDescriptor ("name" , descriptors );
73
+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
74
+ () -> new ServiceDescriptor ("fooService" , descriptors ));
75
+ StringSubject error = assertThat (e ).hasMessageThat ();
76
+ error .contains ("service names" );
77
+ error .contains ("fooService" );
78
+ error .contains ("wrongService" );
81
79
}
82
80
83
81
@ Test
@@ -96,10 +94,9 @@ public void failsOnNonDuplicateNames() {
96
94
.setResponseMarshaller (TestMethodDescriptors .voidMarshaller ())
97
95
.build ());
98
96
99
- thrown .expect (IllegalArgumentException .class );
100
- thrown .expectMessage ("duplicate" );
101
-
102
- new ServiceDescriptor ("name" , descriptors );
97
+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
98
+ () -> new ServiceDescriptor ("name" , descriptors ));
99
+ assertThat (e ).hasMessageThat ().isEqualTo ("duplicate name name/method" );
103
100
}
104
101
105
102
@ Test
0 commit comments