Skip to content

Commit d7a806e

Browse files
author
zahar.kalosha
committed
Bug: QuestionControllerTest have problem with Autowiring
1 parent 3725e60 commit d7a806e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
spring.datasource.url=jdbc:h2:mem:test;MODE=PostgreSQL;
2+
spring.datasource.driver-class-name=org.h2.Driver
3+
spring.datasource.username=${DB_USER}
4+
spring.datasource.password=${DB_PASSWORD}
5+
# We add the MySQL Dialect so that it understands and generates the query based on MySQL
6+
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
7+
8+
spring.h2.console.enabled=true
9+
#spring.jpa.defer-datasource-initialization=true
10+
spring.jpa.hibernate.ddl-auto=update
11+
#spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
12+
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
13+
spring.jpa.properties.hibernate.format_sql=true
14+
#spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
15+
spring.jpa.properties.hibernate.show_sql=true
16+
17+
18+
spring.sql.init.mode=always

0 commit comments

Comments
 (0)