状态返回200,但当我检查日志时,无法看到任何用于创建表或插入数据的h2日志。那么,你建议我怎么做。
我想提到的另一件事是,在测试存储库时,我能够使用@datajpatest注解对h2进行存储库测试。下面您可以看到我的示例属性文件和controllertest文件。
@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);
}
应用程序属性文件,类似于内存中的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
暂无答案!
目前还没有任何答案,快来回答吧!