我正在尝试使用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();
}
}
1条答案
按热度按时间pb3s4cty1#
你可能会遇到这个问题:https://github.com/testcontainers/testcontainers-java/issues/5121
作为解决方法,使用以下URL登录Docker Hub: