在 Spring Boot(MongoDB 测试容器)中运行 IT 测试时测试被忽略并失败

Cla*_*nio 2 integration-testing mongodb maven spring-boot testcontainers

我正在尝试使用 MongoDB 测试容器运行 IT 测试。但是,当我运行测试时,出现以下错误

com.github.dockerjava.api.exception.InternalServerErrorException:状态500:{“message”:“Head”https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0.3.1“:未经授权:用户名或密码不正确”} 位于 org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocalBuilder.execute(DefaultInitationBuilder.java:247) 位于 org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocalBuilder.lambda$在 java.base/java.lang.Thread.run(Thread.java:834) 处执行AndStream$1(DefaultInitationBuilder.java:269)

这是 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Kev*_*tek 8

您可能确实遇到了这个问题: https ://github.com/testcontainers/testcontainers-java/issues/5121

作为解决方法,请使用以下 URL 登录 Docker Hub:

docker login index.docker.io
Run Code Online (Sandbox Code Playgroud)