Home > Blockchain >  Disable SQS listener in test case run
Disable SQS listener in test case run

Time:11-15

Spring Boot 2.7.5
JUnit 4.13.2

I have SQS listener in app:

@Component
@EnableSqs
@Slf4j
public class SdkUploadListener {

    private final S3Service s3Service;

    @Autowired
    public SdkUploadListener(final S3Service s3Service) {
        this.s3Service = s3Service;
    }

    @SqsListener(value = "${amazon.sqs.sdk-upload-queue-url}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    public void processMessage(final S3EventNotification message) throws JsonProcessingException {
      ...
    }
}

There is simple test case (just want to test Airflow run Dag):

@SpringBootTest
@Slf4j
class AirflowClientTest {

    @Autowired
    private AirflowClient airflowClient;

    @Test
    public void testDagRun() {
        final String dagName = "test-dag";
        final OffsetDateTime logicalDate = OffsetDateTime.parse("2022-08-01T00:00:00.000Z");
        final AirflowRunDugRequest request = new AirflowRunDugRequest(logicalDate, Map.of());
        airflowClient.triggerIngestionDag(dagName, request).subscribe(res -> log.info("Response received: {}", res));
    }
}

The problem is that SQS listener starts listening the queue upon test case run.
Is it possible to disable SQS listener for tests?
So I need to use mock for this?

CodePudding user response:

I had a quite similar problem. My solution is a ConditionalOnProperty Annotation at the config class.

@Configuration
@EnableSqs
@ConditionalOnProperty(value = "my.property", havingValue = "true", matchIfMissing = true)
public class MyConfig {
}

With this Annotation you dont have to change any PROD config. For the use in test scope you can place the config value under src/test/resources/config/application.yml.It disable the Config for all Tests.

  • Related