3

I'm trying to test my REST Controller in my Spring Boot application. This is my test class

import com.binar.kelompok3.secondhand.repository.OffersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

@WebMvcTest(HistoryController.class)
public class HistoryControllerTest {

    @MockBean
    private OffersRepository offersRepository;

    @Autowired
    private MockMvc mockMvc;    

    @Test
    void signIn() throws Exception{
        mockMvc.perform(get())
    }
}

When I put @Autowired on the MockMvc field Intellij gives me Could not autowire. No beans of 'MockMvc' type found and the mockMvc.perform method doesn't work. I've tried changing @WebMvcTest to

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc

But Intellij still gives me

Could not autowire. No beans of 'MockMvc' type found

when I hover my cursor on mockMvc

How can I autowired MockMvc? Thanks

BrownFox
  • 103
  • 8
  • What versions of Spring Boot and IntelliJ are you using? If you are using Spring Boot 2.7, you need IntelliJ 2022.1.3 or later. See https://youtrack.jetbrains.com/issue/IDEA-289633 for details. – Andy Wilkinson Jul 23 '22 at 09:39
  • 1
    @AndyWilkinson I'm using Spring Boot 2.7.0 and Intellij 2021.3.3. I'll try to update to the newer version and see if it works –  BrownFox Jul 23 '22 at 16:35

1 Answers1

5

I just needed to update Intellij to 20022.1.3

BrownFox
  • 103
  • 8