java (Resolvido)模拟Mvc始终为空

agxfikkp  于 2023-01-15  发布在  Java
关注(0)|答案(1)|浏览(65)

我试图实现一个控制器测试来保证它会返回http 200代码,但是当试图运行测试时,我收到了一个NullPointerException异常。
调试代码时我发现MockMvc为空。
我已经尝试过添加@SpringBootTest、@AutoConfigureMockMvc、@WebMvcTest、@ExtendWith等注解,但MockMvc仍然为空,现在我不知道还能做些什么来解决这个问题
注意:我正在学习这些测试的东西,所以如果代码中有什么不好的地方,如果你能告诉我,我将不胜感激
下面是我的测试代码:

`
@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());
    }
}

下面是我的控制器代码:

`@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");
        }
    }
}

这是我的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>`
slsn1g29

slsn1g291#

我设法解决了这个问题,发生的事情是注解冲突,导致MockMvc没有被分配。另外,我改变了一些东西,使代码更可读,更好。
代码现在如下所示:

@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();
    }
}

相关问题