8

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?

Jesper
  • 2,644
  • 4
  • 30
  • 65
  • https://stackoverflow.com/a/39869110/643500 – Sully Apr 11 '20 at 01:38
  • @HithamS.AlQadheeb Using `@SpringBootTest` doesn't work for me, as it gives me `jdbc4.CommunicationsException: Communications link failure` – Jesper Apr 11 '20 at 11:15

1 Answers1

8

You can just mock the method that will connect to database in your repository class in the @Test method.

@SpringBootTest
@AutoConfigureMockMvc
class StoreApplicationTests {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CitizenRepository citizenRepository;


    @Test
    void contextLoads() {
    }

    @Test
    public void test() {

        Mockito.when(citizenRepository.getDataFromDB()).thenReturn("Something you'd like to Return");

    }

}

After doing that, citizenRepository.getDataFromDB() will not connect to database when it's called.


Update After Your Comment:

Then you can just create "src/test/resources" and copy your application.properties or application.yml from "src/main/resources" to that directory and comment the mysql connection part.

If you don't have "src/test/resources/application.properties", then spring will read "src/main/resources/application.properties" by default and configure the project according to that file, since you have datasource configuration in it, spring will try to connect to the database, if database server is down, you would get the failure.

Michael Ouyang
  • 1,767
  • 1
  • 11
  • 19
  • 1
    First of all, if I remove `@RunWith(SpringRunner.class)` the autowired beans seem to be null. Secondly, I have already mocked my DB methods. It seems like it never gets to the part where it will execute them though, since I almost immediately get `jdbc4.CommunicationsException: Communications link failure` when trrying to run the test. – Jesper Apr 11 '20 at 11:11
  • Update the Answer. – Michael Ouyang Apr 11 '20 at 12:44
  • 3
    Yes I ended up creating a new application.properties under test, and configuring an H2 in-memory database in it. This solved my problem. – Jesper Apr 11 '20 at 13:05
  • Good to know! BTW, Acceptance is always the greatest encouragement, Thanks! – Michael Ouyang Apr 11 '20 at 13:21