0

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!

3
  • In your pom.xml, did you include com.h2database dependency for test scope? Commented Jul 16, 2018 at 4:58
  • The @WebMvcTest should 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 an EntityManagerFactory (and probably more things) that Spring Boot can configure (and automatically exclude). Commented Jul 16, 2018 at 6:33
  • short answer is yes. long answer is yes, how else do you think all the injection magic will happen? Commented Jul 16, 2018 at 7:01

2 Answers 2

1

Try this.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApp.class)
@EnableWebMvc
@AutoConfigureMockMvc
public class RestControllerMockTest {
@Autowired
HomeRestController homeRestController;

@Before
public void setUp(){
    MockitoAnnotations.initMocks(this);
}

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thaks a lot! Magically AutoConfigureMockMvc=)
1

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)

1 Comment

Thaks for your addition!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.