hc0re
hc0re

Reputation: 1996

(Context) Problems with @RestController testing with JUnit4 and Spring

I have 3 separate projects, bound by pom.xml

main myapp folder with pom.xml having modules section:

<modules>
    <module>myapp-dao</module>
    <module>myapp-webapp</module>
    <module>myapp-configuration</module>
</modules>

3 projects inside myapp folder (each having its own pom.xml)

myapp-dao
myapp-configuration
myapp-webapp

I am writing a JUnit test for a @RestController class in the myapp-webapp module (don't worry about the @Test contents, it is just a skeleton, it will be expanded when I will be able to run the test):

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@WebMvcTest(ContentController.class)
public class ContentControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private ContentService contentService;
    @MockBean
    private HyperlinkReferenceService hyperlinkReferenceService;

    @Test
    public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception {

        given(contentService.findContentUsedAsTemplateIn(1, 0)).willReturn(null);

        mvc.perform(get("/portal/content/1/references/usedAsTemplateIn").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(1)));
    }

}

When I am trying to run the test, I get:

java.lang.IllegalStateException: Failed to load ApplicationContext
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.pro4people.msg.ServletInitializer]; nested exception is java.io.FileNotFoundException: class path resource [myapp.properties] cannot be opened because it does not exist

I have resolved this problem by copying myapp-configuration/target/myapp.properties to myapp-webapp/src/main/test/resources/myapp.properties

This solved the above problem, but another problem came out:

java.lang.IllegalStateException: Failed to load ApplicationContext

Description:
Field userRepository in com.company.myapp.service.UserServiceImpl required a bean of type 'com.company.myapp.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.company.myapp.repository.UserRepository' in your configuration.

But really, I am not even using the UserRepository in this test, so I presume that there is a Spring context problem somewhere around. The application is normally built to a war and deployed to Tomcat, and only in its wared state everything is correctly injected and bound.

How can I omit this problem? Whatever I am trying to do, the test is raising the spring context, and when I remove @WebMvcTest(ContentController.class), I get:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.company.myapp.controller.portal.json.ContentControllerTest': Unsatisfied dependency expressed through field 'mvc'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Which seems to be obvious.

Upvotes: 0

Views: 554

Answers (1)

mrk90
mrk90

Reputation: 66

Use Context configuration on top of your controller test class. @ContextConfiguration(classes = {ContentController.class})

Below code works for unit test for controller.

@RunWith(MockitoJUnitRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ContentController.class})
public class ContentController {

 private MockMvc mockMvc;

 @InjectMocks
 private ContentController contentController;

@Mock
private ContentService contentService;

....

    /**
     * Configure the mockMvc with Controller.
     */
    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(contentController).build();
    }


  @Test
  your test here() {
 //use Mockito.when(statement).thenReturn(value);
  //rest remain same using mockMVC
   mockMvc.perform(...)
}

Upvotes: 2

Related Questions