Теперь я использую этот простой тест:
Код: Выделить всё
@ExtendWith({S3Extension.class})
@ActiveProfiles("test")
@SpringBootTest()
class S3OtherServiceItTest {
@Autowired private S3Service s3Service;
@MockBean SetupSchedulerJobs setupSchedulerJobs;
@MockBean DbStatus dbStatus;
@Test
public void test(AmazonS3Client client) throws IOException, FileException {
File file = new File("test.txt");
Files.write(file.toPath(), "contents".getBytes());
s3Service.uploadLocalFile(file.getPath(), "another/file/name");
client.listObjects("tkgpdf-bucket").getObjectSummaries().forEach(System.out::println);
}
}
Код: Выделить всё
@ExtendWith(SpringExtension.class)
public class S3Extension implements BeforeAllCallback, AfterAllCallback, ParameterResolver {
private S3Mock s3Mock;
private AmazonS3Client s3Client;
private ApplicationProperties applicationProperties;
@Override
public void beforeAll(ExtensionContext context) {
applicationProperties = SpringExtension.getApplicationContext(context).getBean(ApplicationProperties.class);
s3Mock = new S3Mock.Builder().withPort(9000).withFileBackend("build/s3").build();
s3Mock.start();
s3Client = getClient();
s3Client.createBucket(applicationProperties.getS3().getBucket());
}
@Override
public void afterAll(ExtensionContext context) {
if (s3Mock != null) {
s3Mock.shutdown();
}
}
@Override
public boolean supportsParameter(
ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return parameterContext.getParameter().getType() == AmazonS3Client.class;
}
@Override
public Object resolveParameter(
ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return s3Client;
}
public AmazonS3Client getClient() {
AwsClientBuilder.EndpointConfiguration endpoint =
new AwsClientBuilder.EndpointConfiguration(applicationProperties.getS3().getEndpoint(), "us-west-2");
return (AmazonS3Client)
AmazonS3ClientBuilder.standard()
.withPathStyleAccessEnabled(true)
.withEndpointConfiguration(endpoint)
.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()))
.build();
}
}
Не удалось разрешить bean-компонент, поскольку существует два bean-компонента типа ApplicationProperties
Итак, почему создаются два объекта ApplicationProperties Beans и как мне этого избежать? Я подозреваю, что аннотация @SpringBootTest и SpringExtension пытаются создать контекст, приводящий к этой ошибке?
Подробнее здесь: https://stackoverflow.com/questions/790 ... y-integrat