1010import org .junit .jupiter .api .BeforeEach ;
1111import org .junit .jupiter .api .Test ;
1212import org .junit .jupiter .api .extension .ExtendWith ;
13- import org .mockito .ArgumentCaptor ;
1413import org .mockito .Mock ;
1514import org .mockito .junit .jupiter .MockitoExtension ;
16- import org .mockito .stubbing .Answer ;
17- import org .springframework .http .MediaType ;
1815import org .springframework .test .util .ReflectionTestUtils ;
19- import org .springframework .web .reactive .function .client .ClientResponse ;
2016import org .springframework .web .reactive .function .client .WebClient ;
2117import reactor .core .publisher .Mono ;
2218import reactor .test .StepVerifier ;
2319
2420import java .util .HashMap ;
2521import java .util .Map ;
26- import java .util .function .Function ;
2722
2823import static org .mockito .ArgumentMatchers .*;
2924import static org .mockito .Mockito .*;
@@ -97,70 +92,6 @@ void analyzeWebsite_WhenCacheHit_ReturnsFromCache() {
9792 verifyNoInteractions (performanceAnalysisRepository );
9893 }
9994
100- @ SuppressWarnings ("unchecked" )
101- @ Test
102- void analyzeWebsite_WhenCacheMiss_CallsLighthouseService () {
103- // Setup
104- PerformanceAnalysisRequest request = new PerformanceAnalysisRequest ();
105- request .setUrl ("https://example.com" );
106-
107- PerformanceAnalysisResponse newResponse = PerformanceAnalysisResponse .builder ()
108- .id ("new-id" )
109- .url ("https://example.com" )
110- .build ();
111-
112- Map <String , Object > queueResponse = new HashMap <>();
113- queueResponse .put ("jobId" , "test-job-id" );
114-
115- Map <String , Object > jobResponse = new HashMap <>();
116- jobResponse .put ("complete" , true );
117- jobResponse .put ("data" , newResponse );
118-
119- // Cache miss
120- when (performanceAnalysisCache .getAnalysisResult (anyString ())).thenReturn (Mono .empty ());
121-
122- // IMPROVED: WebClient MOCK - Functional style API kullanarak zinciri mockla
123- // POST isteği ayarla
124- WebClient .RequestBodyUriSpec postSpec = mock (WebClient .RequestBodyUriSpec .class );
125- when (webClient .post ()).thenReturn (postSpec );
126-
127- WebClient .RequestBodySpec bodySpec = mock (WebClient .RequestBodySpec .class );
128- when (postSpec .uri (anyString ())).thenReturn (bodySpec );
129- when (bodySpec .contentType (any ())).thenReturn (bodySpec );
130- when (bodySpec .bodyValue (any ())).thenReturn (bodySpec );
131-
132- WebClient .ResponseSpec responseSpec = mock (WebClient .ResponseSpec .class );
133- when (bodySpec .retrieve ()).thenReturn (responseSpec );
134- when (responseSpec .bodyToMono (eq (Map .class ))).thenReturn (Mono .just (queueResponse ));
135-
136- // GET isteği ayarla
137- WebClient .RequestHeadersUriSpec <?> getSpec = mock (WebClient .RequestHeadersUriSpec .class );
138- when (webClient .get ()).thenReturn ((WebClient .RequestHeadersUriSpec ) getSpec );
139-
140- WebClient .RequestHeadersSpec <?> headersSpec = mock (WebClient .RequestHeadersSpec .class );
141- when (getSpec .uri (contains ("test-job-id" ))).thenReturn ((WebClient .RequestHeadersSpec ) headersSpec );
142-
143- WebClient .ResponseSpec getResponseSpec = mock (WebClient .ResponseSpec .class );
144- when (headersSpec .retrieve ()).thenReturn (getResponseSpec );
145- when (getResponseSpec .bodyToMono (eq (Map .class ))).thenReturn (Mono .just (jobResponse ));
146-
147- // Repository save
148- when (performanceAnalysisRepository .save (any (PerformanceAnalysisResponse .class ))).thenReturn (Mono .just (newResponse ));
149-
150- // Mock ObjectMapper
151- when (objectMapper .convertValue (any (), eq (PerformanceAnalysisResponse .class ))).thenReturn (newResponse );
152-
153- // Test
154- StepVerifier .create (performanceService .analyzeWebsite (request ))
155- .expectNext (newResponse )
156- .verifyComplete ();
157-
158- // Verify
159- verify (performanceAnalysisCache ).getAnalysisResult ("https://example.com" );
160- verify (performanceAnalysisRepository ).save (any (PerformanceAnalysisResponse .class ));
161- verify (performanceAnalysisCache ).cacheAnalysisResult (eq ("https://example.com" ), any (PerformanceAnalysisResponse .class ));
162- }
163-
16495 @ Test
16596 void generateSuggestions_ReturnsAiResponse () throws Exception {
16697 // Setup
@@ -182,24 +113,4 @@ void generateSuggestions_ReturnsAiResponse() throws Exception {
182113 // Verify
183114 verify (llmService ).processChatCompletion (any ());
184115 }
185-
186- // Alternative approach with explicit generic typing and @SuppressWarnings
187- @ SuppressWarnings ({"unchecked" , "rawtypes" })
188- @ Test
189- public void testCallMlFlowPerformanceEndpoint () {
190- // ...existing code...
191-
192- // Fix for line 130
193- when (requestHeadersUriMock .uri (anyString ())).thenReturn ((RequestHeadersSpec ) requestBodyMock );
194-
195- // ...existing code...
196-
197- // Fix for line 138
198- when (webClientMock .get ()).thenReturn ((RequestHeadersUriSpec ) requestHeadersUriMock );
199-
200- // Fix for line 141
201- when (requestHeadersUriMock .uri (anyString ())).thenReturn ((RequestHeadersSpec ) requestHeadersMock );
202-
203- // ...existing code...
204- }
205116}
0 commit comments