我在 Spring Boot 应用程序中添加了 testcontainer
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@Testcontainers
@AutoConfigureMockMvc
class ProductServiceApplicationTests {
@Container
static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:4.4.2");
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@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()) ;
}
private ProductRequest getProductRequest() {
return ProductRequest.builder()
.name("Iphone 13")
.description("Iphone 13")
.price(BigDecimal.valueOf(1200))
.build();
}
}
运行时 出现以下错误
com.github.dockerjava.api.exception.InternalServerErrorException:状态500:{“消息”:“头”https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0.3.0“:未经授权: 用户名或密码错误”} 在 org.testcontainers.shaded.com.github.dockerjava.core.DefaultInitationBuilder.execute(DefaultInitationBuilder.java:247) 在 org.testcontainers.shaded.com.github.dockerjava.core.DefaultInitationBuilder.lambda$executeAndStream$1(DefaultInitationBuilder.java:269) 在 java.lang.Thread.run(Thread.java:748)
这对我有帮助:
$ docker logout
$ docker login --username your_username_here
在这里找到它 - https://forums.docker.com/t/unauthorized-in Correct-username-or-password/35677/4
netcfg -d
,然后重新启动电脑。
这对我有用参考
https://github.com/testcontainers/testcontainers-java/issues/3422
在 M1 Mac 上对我有用的修复是:
credsStore:desktop
docker login
对我来说这个解决方案有效(Windows 11)-