首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring @WebMvcTest具有@MockBean空-使用SpringRunner工作

Spring @WebMvcTest具有@MockBean空-使用SpringRunner工作
EN

Stack Overflow用户
提问于 2022-01-20 13:50:14
回答 2查看 817关注 0票数 0

short:从Junit4迁移到JUnit5有困难。删除@RunWith(SpringRunner.class)后,我的@MockBean注释服务为null,尽管我使用的是@WebMvcTest。为什么不起作用?

long:,自从几天以来,我一直在挣扎。我正在对我的Spring应用程序进行一些测试,它使用@RunWith(SpringRunner.class)、im和@WebMvcTest相结合,运行良好。

因为这是JUnit4,所以我试图迁移到Junit5。我从pom中删除了老式的依赖项,并添加了对spring-boot-starter-test的排除。只有junit-jupiter依赖项留给JUnit 5。

我读过一些关于这方面的文章和样本。由于我不想加载完整的上下文,所以SpringBootTest不是那种方式(而且根本不起作用)我“认为”我发现@WebMvcTest就足够了,因为它已经包含了@extendWith注释,这是@RunWith(SpringRunner.class)的JUnit5等价物。所以:我只需要删除springRunner,它就能工作了。

但是在删除了这一行之后,my @MockBean注解服务从现在起为null。我可以使用@WebMvcTest@MockBean找到许多示例。所以我不明白,为什么我的不工作。添加@RunWith(SpringRunner.class)使它再次工作。

这是一个测试类(没有跳马):

代码语言:javascript
复制
package my.package;

import com.fasterxml.jackson.databind.ObjectMapper;
import my.package.controller.SkillTileCommand;
import my.package.controller.SkillTileController;
import my.package.controller.SkillTileResponse;
import my.package.service.SkillTileService;
import org.junit.Test;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(SkillTileController.class)
public class SkillTileControllerIntegrationTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private SkillTileService skillTileService;

    private final SkillTileTestDataFactory testDataFactory = new SkillTileTestDataFactory();

    @Test
    public void givenSkillTiles_whenGETFindAll_thenReturnJsonArray() throws Exception {

        List<SkillTileResponse> skillTiles = testDataFactory.createTestSkillTileResponses(1, true, null, true);
        SkillTileResponse st1 = skillTiles.get(0);

        given(skillTileService.findAll(null)).willReturn(skillTiles);

        mvc.perform(get("/skill-tile/")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$[0].name").value(st1.getName()));
    }
}

相反,使用Spring是这样的:

代码语言:javascript
复制
package my.package;

import com.fasterxml.jackson.databind.ObjectMapper;
import my.package.controller.SkillTileCommand;
import my.package.controller.SkillTileController;
import my.package.controller.SkillTileResponse;
import my.package.service.SkillTileService;
import org.junit.Test;
import org.junit.runner.RunWith;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(SkillTileController.class)
public class SkillTileControllerIntegrationTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private SkillTileService skillTileService;
[...]
}

愿服务主管也感兴趣:

代码语言:javascript
复制
package my.package.service;

import lombok.RequiredArgsConstructor;
import my.package.controller.SkillTileCommand;
import my.package.controller.SkillTileResponse;
import my.package.exception.ResourceNotFoundException;
import my.package.repository.SkillTileEntity;
import my.package.repository.SkillTileRepository;
import my.package.utils.ObjectMapper;
import my.package.utils.ResourceUtility;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class SkillTileService {

    private final SkillTileRepository skillTileRepository;
[...]
}

下面是与测试相关的两个依赖项:

代码语言:javascript
复制
    [...]
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    [...]

谢谢你的帮助。如果不呆在JUnit4就好了。

编辑:我对另一个测试类也有相同的行为,它只使用@DataJpaTest注释,并具有@Autowire字段。在没有@RunWith的情况下为null

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-21 08:39:22

如果您更好地查看您的测试,您将看到您的@Test注释仍然来自旧的org.junit包。那就是JUnit4。这意味着您的测试实际上是用JUnit4而不是JUnit5运行的。它基本上忽略了所有的注释,因此也忽略了@WebMvcTest

要修复,请确保您的@Test注释也来自正确的org.junit.jupiter.api包,使其成为正确的JUnit5测试。

票数 2
EN

Stack Overflow用户

发布于 2022-01-20 14:54:14

您有可能在控制器中使用@RequiredArgsConstructor

确保已将服务字段指定为final@RequiredArgsConstructor文档声明将创建一个构造函数,其中包含未初始化的最终字段或@ will字段的参数。如果没有将SkillTileService依赖项标记为final,则不会将其添加为构造函数参数。

因此,虽然模拟本身不是空的,但它仍然可能永远不会被注入到测试控制器中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70787407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档