【发布时间】:2019-02-11 16:27:48
【问题描述】:
我为 Spring Boot 编写了测试,并且有 2 个类正在测试 API 和存储库。下面提供了骨架,
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class AppointmentAPITest {
/*
* we need a system to simulate this behavior without starting a
* full HTTP server. MockMvc is the Spring class that does that.
* */
@Autowired
private MockMvc mockMvc;
@MockBean
private AppointmentAPI api;
// now the tests are going on
}
存储库测试类,
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class AppointmentRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private AppointmentRepository repository;
// write the test here
}
如何使用一个类来运行它们?例如,如果类是AppointmentApplicationTests,
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppointmentApplicationTests {
@Test
public void contextLoads() {
}
}
配置这个类以便调用 API 和 repo 类中的所有测试的最佳方法是什么?
【问题讨论】:
-
你现在如何运行测试?你能举个例子吗?
-
我从每个文件中单独运行它们。您还需要查看文件中的测试吗?
-
不,我的意思是您只是执行“右键单击 -> 以 Junit 测试方式运行”之类的操作吗?
-
任何构建工具 maven/gradle?
-
你使用IDEA/Eclipse什么IDE?
标签: java spring unit-testing spring-boot integration-testing