1

Status returns 200 but when i checked the logs cannot see any H2 logs for creating tables or inserting data. So, what do you suggest me to do.

Another thing I want to mention is that while testing the Repository, I was able to do my Repository tests with h2 using the @DataJpaTest annotation. Below you can see my sample properties file and controllertest file.

@RunWith(SpringRunner.class)
@WebMvcTest(VehicleController.class)
@ContextConfiguration(classes=DemoApplication.class)
@AutoConfigureMockMvc
public class VehicleControllerTest {

@Autowired
private WebApplicationContext context;

@Autowired
private MockMvc mvc;

@MockBean
private VehicleService vehicleService;

@Before
public void setup() {
    this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}

@Test
public void RetrieveAllVehicles() throws Exception {
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/api/vehicle").content("application/json");
    MvcResult result = this.mvc.perform(requestBuilder).andExpect(status().isOk())
    .andReturn();

    assertEquals(12 , 12);
}

Application Properties file like that for in memory database with H2.

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.datasource.initialization-mode=always
phybarin
  • 81
  • 1
  • 11
  • 2
    this should help: https://stackoverflow.com/questions/63508359/spring-boot-webmvctest-vs-springboottest – pL4Gu33 Apr 10 '21 at 08:10
  • i changed @WebMvcTest(VehicleController.class) with @SpringBootTest . Now, tables are created but still cannot get seed data from data.sql – phybarin Apr 10 '21 at 08:36
  • 4
    So you are using `@MockBean` (which creats a mock, a dummy instance so to speak) and you wonder why there is nothing in your database? Also your test setup doesn't make sense, ditch your `@Before` method as spring boot already does that. That and the `@ContextConfiguration` doesn't add anyuting as well nor does the `@AutoConfigureMockMvc` in this case. If you want a full integration test you should be using `@SpringBootTest` and not `@WebMvcTest`. – M. Deinum Apr 10 '21 at 10:05
  • Yes you are right i fixed the issue, After i posted it with these changes. Thanks for helps – phybarin Apr 10 '21 at 11:05

0 Answers0