mockMvc beans在springboot 3.2.2的集成测试中始终为空

问题描述 投票:0回答:1

在 springboot 3.2.2 的集成测试中,我的模拟存储库 bean 始终为 null 我不明白为什么我必须模拟所有依赖的 bean。有没有办法自动完成? 有时mockMvc也为空。

我已尝试删除

@WebMvcTest(DeliveryController.class)

并使用以下内容:

@RunAs("SpringRunner.class")
@SpringBootTest
@AutoConfigureMockMvc

但徒劳。 mockMvc 始终为空。

我不想添加以下初始化代码,因为mockMvc不应该为空

@BeforeEach
void setUp() {
    this.mockMvc = MockMvcBuilders.standaloneSetup(new DeliveryController(new DeliveryServiceImpl())).build();
}
@WebMvcTest(DeliveryController.class)
public class DeliveryControllerTest {

    @Autowired
    private static MockMvc mockMvc;

    @MockBean
    private DeliveryRepository deliveryRepository; // Mocking the repository

    @MockBean
    private UserRepository userRepository; // Mocking the repository

    @Test
    public void testCreateDelivery() throws Exception {
        DeliveryEntity delivery = DeliveryEntity.builder().deliveryId(1L).description("Test package").build();

        Mockito.when(deliveryRepository.save(Mockito.any(DeliveryEntity.class))).thenReturn(delivery);

        mockMvc.perform(get("/deliveries/1").contentType(MediaType.APPLICATION_JSON).content("{\"description\": \"Test package\"}")) // Use a JSON payload that matches your Delivery object structure
                .andExpect(status().isOk());// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));

        Mockito.verify(deliveryRepository, Mockito.times(1)).save(Mockito.any(DeliveryEntity.class));
    }

    // Add more test methods for other endpoints
}

如有任何帮助,我们将不胜感激。

mockMvc 不应为 null,存储库 bean 也不应为 null。

这是我在pom文件中的依赖项

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </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-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-docker-compose</artifactId> 
            <scope>runtime</scope> <optional>true</optional> </!dependency -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.4.1.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>

</dependencies>
``
spring-boot spring-mvc mockito
1个回答
0
投票

自动装配字段不能是静态的,它们必须是实例字段。

所以改变

@Autowired
private static MockMvc mockMvc;

@Autowired
private MockMvc mockMvc;
© www.soinside.com 2019 - 2024. All rights reserved.