Jesper
Jesper

Reputation: 2814

Spring boot test without database connection

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?

Upvotes: 11

Views: 28472

Answers (1)

Michael Ouyang
Michael Ouyang

Reputation: 1827

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.

Upvotes: 8

Related Questions