P
P
postya2020-06-02 11:43:27
Software testing
postya, 2020-06-02 11:43:27

How to test controller layer in Spring Boot with Mockito?

My Spring Boot application consists of three layers: Repository(DB) , Service(Business logic) and Controller

I have written one method so far that retrieves all records from the database

How to test the findAll method in the controller layer with Mockito?

What the project consists of:

Model:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "video")
public class Video {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String title;
    private String url;
}


repository:
@Repository
public interface VideoRepository extends JpaRepository<Video, Integer> {
}

service:
public interface VideoService {
    List<Video> findAll();
}


VideoServiceImpl:
@Service
public class VideoServiceImpl implements VideoService{

    @Autowired
    private VideoRepository videoRepository;

    @Override
    public List<Video> findAll() {
        return videoRepository.findAll();
    }
}


findAll method in Service layer tested successfully:
class VideoServiceImplTest {

    @InjectMocks
    VideoServiceImpl videoService;

    @Mock
    VideoRepository videoRepository;

    private Video videoOne = new Video(1, "kultas", "https://webnsdf");
    private Video videoTwo = new Video(2, "joshua", "https://343heth");
    private Video videoThree = new Video(3, "ambient mix", "https://ambientsome");

    @BeforeEach
    public void init() throws Exception {
        MockitoAnnotations.initMocks(this);
    }


    @Test
    void findAll() {

        List<Video> list = new ArrayList<>();

        list.add(videoOne);
        list.add(videoTwo);
        list.add(videoThree);

        when(videoRepository.findAll()).thenReturn(list);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
postya, 2020-06-03
@postya

Found this solution:

import ga.rusanov.backend.model.Video;
import ga.rusanov.backend.service.VideoService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(controllers = VideoController.class)
class VideoControllerTest {


    @MockBean
    private VideoService videoService;

    @Autowired
    private MockMvc mockMvc;

    private List<Video> videoList;

    @BeforeEach()
     void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        this.videoList = new ArrayList<>();
        this.videoList.add(new Video(1, "kultas", "https://webnsdf"));
        this.videoList.add(new Video(2, "joshua", "https://343heth"));
        this.videoList.add(new Video(3, "ambient mix", "https://ambientsome"));
    }

    @Test
    void shouldGetAllVideos() throws Exception{

        Mockito.when(videoService.findAll()).thenReturn(videoList);

        given(videoService.findAll()).willReturn(videoList);
        this.mockMvc.perform(get("/api/video/all"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.*", hasSize(3)));
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question