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

8hhllhi2  于 2023-05-06  发布在  Maven
关注(0)|答案(1)|浏览(229)

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

com.github.dockerjava.API.exception.InternalServerErrorException:状态500:{“message”:“Head“https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0.3.1“:unauthorized:错误的用户名或密码”} at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:247)at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)at 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();
    }
}
pb3s4cty

pb3s4cty1#

你可能会遇到这个问题:https://github.com/testcontainers/testcontainers-java/issues/5121
作为解决方法,使用以下URL登录Docker Hub:

docker login index.docker.io

相关问题