【发布时间】:2020-07-23 19:39:16
【问题描述】:
起初我的测试类上方有以下注释:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
使用该配置,它会尝试连接到我的数据库,如果我的数据库未运行,则会出现此错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
我希望我的测试在没有任何数据库连接的情况下运行,这就是我尝试更改注释的原因,所以我的测试类现在看起来像这样:
@RunWith(SpringRunner.class)
@DataJpaTest
@WebMvcTest(CitizenController.class)
@AutoConfigureMockMvc
public class CitizenControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CitizenRepository citizenRepository;
@MockBean
private WeeklyCareRepository weeklyCareRepository;
@MockBean
private SubCategoryCareRepository subCategoryCareRepository;
@Autowired
private ObjectMapper objectMapper;
private static List<Citizen> mockCitizenList;
private String citizenJson;
但是,我现在遇到另一个错误:
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [controllers.CitizenControllerTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper)]
是否可以在没有数据库连接的情况下运行我的测试?如果是这样,我做错了什么/错过了什么?
【问题讨论】:
-
@HithamS.AlQadheeb 使用
@SpringBootTest对我不起作用,因为它给了我jdbc4.CommunicationsException: Communications link failure
标签: java spring spring-boot testing