(Resolvido) Mock Mvc is always null

177 Views Asked by At

I was trying to implement a controller test to guarantee that it would return the http 200 code, but when trying to run the test I received a NullPointerException exception.

Debugging the code I found that the MockMvc was null.

I already tried to add annotations like @SpringBootTest, @AutoConfigureMockMvc, @WebMvcTest, @ExtendWith and the MockMvc was still null and now I don't know what else to do to solve this problem

Note: I'm learning about these test things so if there's something bad in the code I'd appreciate it if you let me know

Here is my test code:

`
@ExtendWith(MockitoExtension.class)
@WebMvcTest(BookController.class)
@AutoConfigureMockMvc
@SpringBootTest
public class TestControlBook {

    @Autowired
    private MockMvc mockMvc;


    @Test
    public void testListBooks() throws Exception{
        Response ResultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/books/list"));
        response.andExpect(MockMvcResultMatchers.status().isOk());
    }
}

here is my controller code:


`@RestController
@RequestMapping("/api/books")
public class BookController {


    private BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }



    @GetMapping("/list")
    private ResponseEntity<Object> listBooks(){
        try {
            return ResponseEntity.status(HttpStatus.OK).body(bookService.listBooks());
        }
        catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while trying to list the books, please try again later");
        }
    }
}

and here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tdd</groupId>
    <artifactId>exemplo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>TDD Exemplo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.2.0</version>
        </dependency>


        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>4.6.1</version>
            <scope>test</scope>

        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>`
```
1

There are 1 best solutions below

1
On

I managed to solve the problem, what was happening was that the annotations were in conflict, causing the MockMvc not to be assigned. In addition, I changed some things so that the code was more readable and better.

The code is now like this:

@WebMvcTest(LivroController.class)
@AutoConfigureMockMvc
public class LivroControlerTeste {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private LivroService livroService;

    @Test
    public void testarListarLivros() throws Exception{
       MvcResult mvcResult = mockMvc.perform(get("/api/livros/listar"))
               .andExpect(status().isOk()).andReturn();
    }
}