I do not really understand how to properly use the tests in Spring. Do I really need to perform a full initialization of the entire Spring configuration to run the test?
I have a final integration test, a normal controller call, and checking its response. I have to do it this way:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RestControllerTest {
@Autowired
HomeRestController homeRestController;
@Test
@Repeat(value = 15)
public void test() throws Exception {
RequestSearch requestSearch = new RequestSearch();
HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
requestSearch.setMonth(7);
requestSearch.setYear(2018);
requestSearch.setGbNumber(5010);
requestSearch.setPayTime(new Double(32));
requestSearch.setScanTime(new Double(2.7));
requestSearch.setWaitTime(new Double(35));
ReportResponse reportResponse = homeRestController.find(requestSearch, httpServletResponse);
Assert.assertTrue(reportResponse.cashBoxPlans.size() == 1487);
Assert.assertTrue(reportResponse.getExcelPath().equals("ExceReport.xls"));
}
}
Having looked at the Spring documentation I found one of the perfect examples and wanted to add a separate class with the test:
@RunWith(SpringRunner.class)
@WebMvcTest(HomeRestController.class)
public class RestControllerMockTest {
@Autowired
private MockMvc mvc;
@Test
public void test() throws Exception {
RequestSearch requestSearch = new RequestSearch();
requestSearch.setMonth(7);
requestSearch.setYear(2018);
requestSearch.setGbNumber(5010);
requestSearch.setPayTime(new Double(32));
requestSearch.setScanTime(new Double(2.7));
requestSearch.setWaitTime(new Double(35));
mvc.perform(post(HomeRestController.postUrl,requestSearch).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
But this test does not work, this is the exception that is being thrown:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [ru/denisa/configuration/SQLServerDatabaseConfiguration.class]:
The configruation class mentioned in the exception (SQLServerDatabaseConfiguration) is a configuration class annotated with @Configuration.
If I add the following annotation to my test:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Then I get the following exception:
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [ru.denisa.test.service.rest.RestControllerMockTest]:
How to do it right? Thanks!
com.h2databasedependency for test scope?@WebMvcTestshould only construct a context containing a minimal web configuration and dependencies for the controller should be mocked using@MockBean. The main issue is that you are configuring things which Spring Boot should be configuring. Why are you manually configuring anEntityManagerFactory(and probably more things) that Spring Boot can configure (and automatically exclude).