运行 在 Spring 启动时进行 IT 测试时测试被忽略并失败(MongoDB 测试容器)

Test ignored and failing when running IT test in Spring Boot (MongoDB Test Container)

我正在尝试 运行 使用 MongoDB 测试容器进行 IT 测试。但是,当我 运行 测试

时出现以下错误

com.github.dockerjava.api.exception.InternalServerErrorException:状态 500:{“消息”:“标题”https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0。 3.1": 未授权:不正确的用户名或密码"} 在 org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:247) 在 org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269) 在 java.base/java.lang.Thread.run(Thread.java:834)

这是 IT 测试:

@Testcontainers
@AutoConfigureMockMvc
public class ProductIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ProductRepository productRepository;

    // Step 1 - Download mongodb test container
    @Container
    static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:4.0.10");

    // Step 2 - Add ReplicaSetUrl dynamically (We don't want to connect to real DB, but the test container)
    @DynamicPropertySource
    static void setProperties(DynamicPropertyRegistry dynamicPropertyRegistry) {
        dynamicPropertyRegistry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
    }

    @Test
    void shouldCreateProduct() throws Exception {
        ProductRequest productRequest = getProductRequest();
        String productRequestString = objectMapper.writeValueAsString(productRequest);
        mockMvc.perform(MockMvcRequestBuilders.post("/api/product")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(productRequestString))
                .andExpect(status().isCreated());
        Assertions.assertEquals(1, productRepository.findAll().size());
    }

    private ProductRequest getProductRequest() {
        return ProductRequest.builder()
                .name("iPhone 13")
                .description("iPhone 13")
                .price(BigDecimal.valueOf(1200))
                .build();
    }
}

您可能 运行 解决了这个问题: https://github.com/testcontainers/testcontainers-java/issues/5121

作为解决方法,请使用 URL:

登录 Docker Hub
docker login index.docker.io