4545import org .junit .jupiter .api .AfterAll ;
4646import org .junit .jupiter .api .BeforeAll ;
4747import org .junit .jupiter .api .Test ;
48+ import org .junit .jupiter .api .extension .ExtendWith ;
4849import org .junit .jupiter .api .extension .RegisterExtension ;
50+ import org .mockito .Mock ;
51+ import org .mockito .junit .jupiter .MockitoExtension ;
4952
53+ @ ExtendWith (MockitoExtension .class )
5054class TwilioClientTest {
5155
5256 @ RegisterExtension
@@ -119,7 +123,9 @@ class TwilioClientTest {
119123 + " \" more_info\" : \" Testing\" \n "
120124 + " }" ;
121125
122- private TwilioRestClient twilioRestClient ;
126+ @ Mock private TwilioRestClient twilioRestClient ;
127+
128+ @ Mock private CloseableHttpClient httpClient ;
123129
124130 @ BeforeAll
125131 static void setUp () {
@@ -135,7 +141,6 @@ static void tearDown() {
135141
136142 @ Test
137143 void synchronousMessage () {
138- twilioRestClient = mock (TwilioRestClient .class );
139144 when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
140145 when (twilioRestClient .request (any ()))
141146 .thenReturn (
@@ -162,6 +167,7 @@ void synchronousMessage() {
162167 span ->
163168 span .hasName ("MessageCreator.create" )
164169 .hasKind (CLIENT )
170+ .hasParent (trace .getSpan (0 ))
165171 .hasAttributesSatisfyingExactly (
166172 equalTo (
167173 stringKey ("twilio.type" ),
@@ -174,7 +180,6 @@ void synchronousMessage() {
174180
175181 @ Test
176182 void synchronousCall () throws URISyntaxException {
177- twilioRestClient = mock (TwilioRestClient .class );
178183 when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
179184 when (twilioRestClient .request (any ()))
180185 .thenReturn (
@@ -201,6 +206,7 @@ void synchronousCall() throws URISyntaxException {
201206 span ->
202207 span .hasName ("CallCreator.create" )
203208 .hasKind (CLIENT )
209+ .hasParent (trace .getSpan (0 ))
204210 .hasAttributesSatisfyingExactly (
205211 equalTo (
206212 stringKey ("twilio.type" ), "com.twilio.rest.api.v2010.account.Call" ),
@@ -212,9 +218,8 @@ void synchronousCall() throws URISyntaxException {
212218
213219 @ Test
214220 void httpClient () throws IOException {
215- CloseableHttpClient httpClient = mock (CloseableHttpClient .class );
216- CloseableHttpResponse response1 = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
217- when (httpClient .execute (any ())).thenReturn (response1 );
221+ CloseableHttpResponse response = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
222+ when (httpClient .execute (any ())).thenReturn (response );
218223
219224 HttpClientBuilder clientBuilder = getHttpClientBuilder (httpClient );
220225
@@ -245,6 +250,7 @@ void httpClient() throws IOException {
245250 span ->
246251 span .hasName ("MessageCreator.create" )
247252 .hasKind (CLIENT )
253+ .hasParent (trace .getSpan (0 ))
248254 .hasAttributesSatisfyingExactly (
249255 equalTo (
250256 stringKey ("twilio.type" ),
@@ -264,7 +270,6 @@ void httpClient() throws IOException {
264270
265271 @ Test
266272 void httpClientRetry () throws IOException {
267- CloseableHttpClient httpClient = mock (CloseableHttpClient .class );
268273 CloseableHttpResponse response1 = mockResponse (ERROR_RESPONSE_BODY , 500 );
269274 CloseableHttpResponse response2 = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
270275 when (httpClient .execute (any ())).thenReturn (response1 , response2 );
@@ -297,6 +302,7 @@ void httpClientRetry() throws IOException {
297302 span -> span .hasName ("test" ).hasNoParent ().hasAttributes (Attributes .empty ()),
298303 span ->
299304 span .hasName ("MessageCreator.create" )
305+ .hasParent (trace .getSpan (0 ))
300306 .hasKind (CLIENT )
301307 .hasAttributesSatisfyingExactly (
302308 equalTo (
@@ -310,7 +316,6 @@ void httpClientRetry() throws IOException {
310316
311317 @ Test
312318 void httpClientRetryAsync () throws Exception {
313- CloseableHttpClient httpClient = mock (CloseableHttpClient .class );
314319 CloseableHttpResponse response1 = mockResponse (ERROR_RESPONSE_BODY , 500 );
315320 CloseableHttpResponse response2 = mockResponse (MESSAGE_RESPONSE_BODY , 200 );
316321 when (httpClient .execute (any ())).thenReturn (response1 , response2 );
@@ -352,6 +357,7 @@ void httpClientRetryAsync() throws Exception {
352357 span ->
353358 span .hasName ("MessageCreator.createAsync" )
354359 .hasKind (CLIENT )
360+ .hasParent (trace .getSpan (0 ))
355361 .hasAttributesSatisfyingExactly (
356362 equalTo (
357363 stringKey ("twilio.type" ),
@@ -364,7 +370,6 @@ void httpClientRetryAsync() throws Exception {
364370
365371 @ Test
366372 void syncFailure () {
367- twilioRestClient = mock (TwilioRestClient .class );
368373 when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
369374 when (twilioRestClient .request (any ()))
370375 .thenReturn (
@@ -395,13 +400,13 @@ void syncFailure() {
395400 span ->
396401 span .hasName ("MessageCreator.create" )
397402 .hasKind (CLIENT )
403+ .hasParent (trace .getSpan (0 ))
398404 .hasStatus (StatusData .error ())
399405 .hasException (new ApiException ("Testing Failure" ))));
400406 }
401407
402408 @ Test
403409 void rootSpan () {
404- twilioRestClient = mock (TwilioRestClient .class );
405410 when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
406411 when (twilioRestClient .request (any ()))
407412 .thenReturn (
@@ -436,7 +441,6 @@ void rootSpan() {
436441
437442 @ Test
438443 void asynchronousCall () throws Exception {
439- twilioRestClient = mock (TwilioRestClient .class );
440444 when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
441445 when (twilioRestClient .request (any ()))
442446 .thenReturn (
@@ -472,6 +476,7 @@ void asynchronousCall() throws Exception {
472476 span ->
473477 span .hasName ("MessageCreator.createAsync" )
474478 .hasKind (CLIENT )
479+ .hasParent (trace .getSpan (0 ))
475480 .hasAttributesSatisfyingExactly (
476481 equalTo (
477482 stringKey ("twilio.type" ),
@@ -484,7 +489,6 @@ void asynchronousCall() throws Exception {
484489
485490 @ Test
486491 void asynchronousError () {
487- twilioRestClient = mock (TwilioRestClient .class );
488492 when (twilioRestClient .getObjectMapper ()).thenReturn (new ObjectMapper ());
489493 when (twilioRestClient .request (any ()))
490494 .thenReturn (
@@ -523,6 +527,7 @@ void asynchronousError() {
523527 span ->
524528 span .hasName ("MessageCreator.createAsync" )
525529 .hasKind (CLIENT )
530+ .hasParent (trace .getSpan (0 ))
526531 .hasStatus (StatusData .error ())
527532 .hasException (new ApiException ("Testing Failure" ))));
528533 }
0 commit comments