Reputation: 171
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!
Upvotes: 0
Views: 1531
Reputation: 1750
a little bit simpler code:
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HomeRestController.class)
public class RestControllerMockTest {
}
PS. for disable security just change @WebMvcTest(controllers = HomeRestController.class, secure= false)
Upvotes: 1
Reputation: 321
Try this.
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApp.class)
@EnableWebMvc
@AutoConfigureMockMvc
public class RestControllerMockTest {
@Autowired
HomeRestController homeRestController;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
}
Upvotes: 1