At first I had the following annotation above my test class:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
With that configuration it tries to connect to my database, which will give me this error if my database is not running:
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)
I would like my test to run without any connection to a database, which is why I tried to change the annotations, so my test class now looks like this:
@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;
However, I am now getting another error:
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)]
Is it possible to run my test without a database connection? If so, what am I doing wrong/missing?