|
| 1 | +package com.example.postgresdemo.controller; |
| 2 | + |
| 3 | +import com.example.postgresdemo.model.Question; |
| 4 | +import com.example.postgresdemo.repository.QuestionRepository; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 10 | +import org.springframework.test.web.servlet.MockMvc; |
| 11 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 12 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 13 | +import org.springframework.web.context.WebApplicationContext; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertNotNull; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +//@SpringBootTest |
| 21 | +//@WebMvcTest |
| 22 | +@WebMvcTest(QuestionController.class) |
| 23 | +public class QuestionControllerTest { |
| 24 | +// private QuestionRepository questionRepository; |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private WebApplicationContext webApplicationContext; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private MockMvc mockMvc; |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testCreateMockMvc() { |
| 34 | + assertNotNull(mockMvc); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testGetQuestions() throws Exception { |
| 39 | +// when(questionRepository.findAll()).thenReturn(List.of(new Question())); |
| 40 | + |
| 41 | + this.mockMvc |
| 42 | + .perform(MockMvcRequestBuilders.get("/questions")) |
| 43 | + .andExpect(MockMvcResultMatchers.status().isOk()); |
| 44 | + } |
| 45 | +} |
0 commit comments