3

I am using a dependent module called spring-cloud-aws. It has a @Configuration class as org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration In my SpringBoot JUnit test case the SqsConfiguration class is getting detected and Beans are getting initialized. I want to exclude this Configuration in class in my JUNit test case. How to achieve this ?

I tried using @ComponentScan it didn't work.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SQLTestConfig.class)
@ActiveProfiles("test")
public class BusinessManagerTest {

}

@TestConfiguration
@ComponentScan(basePackages = {"package1","package1"},
excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = SqsConfiguration.class)})
@Profile("test")
class SQLTestConfig {   

    @Bean
    public SomeBean beans() {

        return new SomeBean();
    }



}

Loading this configuration class requires aws credentials to be available. I don't want to inject credentials for running a simple Bean test case.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Invocation of init method failed; nested exception is com.amazonaws.services.sqs.model.AmazonSQSException: The security token included in the request is expired

user91604
  • 83
  • 1
  • 6

2 Answers2

0

There are multiple ways to exclude specific auto-configuration during testing:

  • exclude via properties in your application-test.properties
spring.autoconfigure.exclude=org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration
  • exclude via @TestPropertySource:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = SQLTestConfig.class)
@TestPropertySource(properties ="spring.autoconfigure.exclude=org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration")

  • exclude via @EnableAutoConfiguration, e.g.:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = SQLTestConfig.class)
@EnableAutoConfiguration(exclude=SqsConfiguration.class)

Choose one that suites you better ;)

Oleksii Zghurskyi
  • 3,967
  • 1
  • 16
  • 17
  • I actually tried all of these. I got an error "The following class(SqsConfiguration) is not an Auto configuration class to exclude". Not sure what to do. – user91604 Sep 08 '19 at 20:38
  • @user91604 Yes indeed. I just blindly assumed `SqsConfiguration` is auto-configuration (facepalm). I checked SqsConfiguration on github - it turns out 2 configs are used to enable SQS - `SqsClientConfiguration` and `SqsConfiguration`. Let's try to exclude them both in `excludeFilters` ? – Oleksii Zghurskyi Sep 09 '19 at 13:05
  • @user91604 Also if suggestion with exclusion doesn't work, let's try approach from [here](https://github.com/spring-cloud/spring-cloud-aws/issues/225#issuecomment-303674594). Seems like it's common problem and guys figured out to mock SQS for tests. – Oleksii Zghurskyi Sep 09 '19 at 13:10
0

So to disable the auto-loading of all Beans for a Test, the test class can explicitly mention the dependencies required. This can be done using ContextConfiguration annotation. eg,

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {EmployeeService.class})
public class EmployeeLeavesTest { 

   @Autowired
   private EmployeeService employeeService;

}

In this eg, only EmployeeService class will be available and other beans will not be loaded.

Isaac Philip
  • 498
  • 1
  • 5
  • 14