当前位置: 首页 > 知识库问答 >
问题:

@mockbean在使用JUnit5和Sping Boot2的@webmvctest中不起作用?

慎峻
2023-03-14

我研究了StackOverflow的类似问题,但在我的案例中找不到根本原因。

上下文:

我在SpringBoot2中有@RestController依赖于BookSearcherService。我想模拟booksearcherservice以便单元测试Controller。

代码:

BookSearcherController.java:

@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookSearcherController {

    private BookSearcherService bookSearcherService;

    @GetMapping(path = "/list", produces = { "application/json" })
    @ResponseBody
    public List<BookDto> listAll() {
        return bookSearcherService.listAll();
    }
}

BookSearcherService.java:

@Service
public class BookSearcherService {

    private BookSearcherCrudService bookSearcherCrudService;

    @Autowired
    void setBookSearcherRepository(BookSearcherCrudService bookSearcherCrudService) {
        this.bookSearcherCrudService = bookSearcherCrudService;
    }

    private BookSearcherRepository bookSearcherRepository;

    @Autowired
    void setBookSearcherRepository(BookSearcherRepository bookSearcherRepository) {
        this.bookSearcherRepository = bookSearcherRepository;
    }

    public List<BookDto> listAll() {
        List<Book> books = bookSearcherRepository.findAll();
        return books.stream()
                .map(bookSearcherCrudService::toDto)
                .collect(Collectors.toList());
    }
}
@WebMvcTest(controllers = BookSearcherController.class)
public class BookSearcherControllerMockMvcTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private BookSearcherService bookSearcherService;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void canListAll() throws Exception {
        assertThat(bookSearcherService).isNotNull();
        assertThat(mockMvc).isNotNull();

        List<BookDto> books = asList(new BookDto("Title 1"), new BookDto("Title 2"), new BookDto("Title 3"));

        given(bookSearcherService.listAll()).willReturn(books);

        ResultActions perform = mockMvc.perform(get("/book/list"));
    }
}
<?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 http://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.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.search.book</groupId>
    <artifactId>book-searcher</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>book-searcher</name>
    <description>Project for books search</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

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

</project>

共有1个答案

尉迟明辉
2023-03-14

问题是booksearcherservice没有被注入到booksearchcontroller中,因为它使用了@requiredargsconstructor,在本例中,它没有创建正确的构造函数。

@requiredargsconstructor文档指出,将使用未初始化的final@nonnull字段的参数创建构造函数。但是,booksearcherservice两者都不是,因此它不作为构造函数参数添加。

因此,尽管模拟本身不是空的,但它从未被注入到被测试的控制器中。

 类似资料: