의존성 주입
This commit is contained in:
@@ -1,24 +1,9 @@
|
||||
package com.example.eatgo.domain;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RestaurantRepository {
|
||||
public interface RestaurantRepository {
|
||||
List<Restaurant> findAll();
|
||||
|
||||
private List<Restaurant> restaurantList = new ArrayList<>();
|
||||
|
||||
public RestaurantRepository() {
|
||||
restaurantList.add(new Restaurant(1004L, "Bob zip", "Seoul"));
|
||||
restaurantList.add(new Restaurant(2020L, "Cyber Food", "Seoul"));
|
||||
}
|
||||
|
||||
public List<Restaurant> findAll() {
|
||||
return restaurantList;
|
||||
}
|
||||
|
||||
public Restaurant findById(Long id) {
|
||||
return restaurantList.stream().filter(v -> v.getId().equals(id)).findFirst().orElse(null) ;
|
||||
}
|
||||
Restaurant findById(Long id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.eatgo.domain;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public class RestaurantRepositoryImpl implements RestaurantRepository {
|
||||
|
||||
private List<Restaurant> restaurantList = new ArrayList<>();
|
||||
|
||||
public RestaurantRepositoryImpl() {
|
||||
restaurantList.add(new Restaurant(1004L, "Bob zip", "Seoul"));
|
||||
restaurantList.add(new Restaurant(2020L, "Cyber Food", "Seoul"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Restaurant> findAll() {
|
||||
return restaurantList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Restaurant findById(Long id) {
|
||||
return restaurantList.stream().filter(v -> v.getId().equals(id)).findFirst().orElse(null) ;
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,19 @@ package com.example.eatgo.interfaces;
|
||||
|
||||
import com.example.eatgo.domain.Restaurant;
|
||||
import com.example.eatgo.domain.RestaurantRepository;
|
||||
import com.example.eatgo.domain.RestaurantRepositoryImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class RestaurantController {
|
||||
|
||||
private final RestaurantRepository repository = new RestaurantRepository();
|
||||
private final RestaurantRepository repository;
|
||||
|
||||
@GetMapping("/restaurants")
|
||||
public List<Restaurant> list() {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.example.eatgo.interfaces;
|
||||
|
||||
import com.example.eatgo.domain.RestaurantRepository;
|
||||
import com.example.eatgo.domain.RestaurantRepositoryImpl;
|
||||
import org.hamcrest.core.StringContains;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@@ -16,9 +19,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
@WebMvcTest(RestaurantController.class)
|
||||
class RestaurantControllerTest {
|
||||
|
||||
@Autowired
|
||||
@ Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@SpyBean(RestaurantRepositoryImpl.class)
|
||||
private RestaurantRepository restaurantRepository;
|
||||
|
||||
@Test
|
||||
public void list() throws Exception {
|
||||
mvc.perform(get("/restaurants"))
|
||||
|
||||
Reference in New Issue
Block a user