Home > Enterprise >  Spring boot WebMvcTest cases keep getting terminated and won't run
Spring boot WebMvcTest cases keep getting terminated and won't run

Time:02-26

When I try using the below code to test the controllers no errors happen, it just says terminated with no logged messages or anything. enter image description here

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import ....controllers.UserController;
import ....data.response.UserResponse;
import ....models.user.User;

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserWebMvc {

    @Autowired
    private MockMvc mvc;

    @Test
    public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception {

    User alex = new User();

    List<UserResponse> allUsers = Arrays.asList(new UserResponse(alex.getId(), alex.getInfo()));

    
    RequestBuilder request = MockMvcRequestBuilders.get("/hello");
    MvcResult result = mvc.perform(request).andReturn();
    
    assertEquals(result.getResponse().getContentAsString(), "hello");
//  userService.createUser(
//      new UserRequest(alex.getInfo().getName(), alex.getInfo().getEmail(), alex.getInfo().getPassword()));
//  
    }
}

However my test to check that junit is working runs fine, so I'm thinking its something to do with SpringRunner or WebMvc

@SpringBootTest
class BackendApplicationTests {

    @Test
    void contextLoads() {
    assertTrue(false);
    }

}

CodePudding user response:

Isn't assertTrue(false) always going to be false? You are basically checking whether false equals true, which will always fail.

Also, from the Spring documentation regarding @WebMvcTest:

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (i.e. @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver beans but not @Component, @Service or @Repository beans).

Using this annotation, it will not scan any beans from the service layer. So if you have a UserService which is injected into the UserController, it will not be found by Spring.

When I run tests like this, I would do something like:

class UserControllerTest {

    @InjectMocks
    private UserController userController;

    private MockMvc mockMvc;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
        mockMvc = 
           MockMvcBuilders.standaloneSetup(userController).build();
    }

    @Test
    public void givenEmployees_whenGetEmployees_thenReturnJsonArray() 
        throws Exception {

        User alex = new User();
        List<UserResponse> allUsers = Arrays.asList(new 
            UserResponse(alex.getId(), alex.getInfo()));
        RequestBuilder request = MockMvcRequestBuilders.get("/hello");
        MvcResult result = mvc.perform(request).andReturn();
        assertEquals(result.getResponse().getContentAsString(), 
             "hello");
    }
}

CodePudding user response:

I was using the wrong import. Changing to this fixed it

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
  • Related