Skip to content

Commit

Permalink
test: #101 자동완성 controller test작성
Browse files Browse the repository at this point in the history
  • Loading branch information
PARKJONGGYEONG committed May 12, 2024
1 parent f49db25 commit 81b24fb
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.t3t.bookstoreapi.elastic.controller;

import com.t3t.bookstoreapi.elastic.service.AutocompleteService;
import com.t3t.bookstoreapi.model.response.BaseResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

class AutocompleteControllerTest {

@Mock
private AutocompleteService autocompleteService;

@InjectMocks
private AutocompleteController autocompleteController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void autocomplete_ReturnsExpectedResults() throws IOException {
// Given
String prefix = "test";
List<String> expectedData = Arrays.asList("example");
BaseResponse<List<String>> serviceResponse = new BaseResponse<>();
serviceResponse.setData(expectedData);

when(autocompleteService.autocomplete(prefix)).thenReturn(serviceResponse);

// When
ResponseEntity<BaseResponse<List<String>>> response = autocompleteController.autocomplete(prefix);

// Then
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(expectedData, response.getBody().getData());
}
}

0 comments on commit 81b24fb

Please sign in to comment.