0

I have a Test class:

@ComponentScan(basePackageClasses = {MyAppConfig.class}, excludeFilters = {@ComponentScan.Filter(classes = {OtherTest.ConflictsWithAppTestConfig.class})})
...
public class MyAppTest { ... }

and an additional unrelated Test class:

public class OtherTest {
    @Configuration
    public static class ConflictsWithAppTestConfig { ... }
}

with the following directory structure:

src/java/com/example
    \_ MyAppConfig.java
    \_ somesubdirectory
        \_ <more spring components>
        ...

test/java/com/example
    \_ MyAppTest.java
    \_ OtherTest.java // with OtherTest.ConflictsWithAppTestConfig

The @ComponentScan.Filter allows me to stop importing the OtherTest.ConflictsWithAppTestConfig in MyAppTest when using @ComponentScan.

This works, however as I add more test cases under the same base, maintaining this list of excluded classes will grow with respect to other test classes under the same package with @ComponentScan.

How can I setup the @ComponentScan filter to only include src directory configurations to let me import test configurations manually?

i.e. fill in the blanks:

@ComponentScan(..., excludeFilters = {@ComponentScan.Filter(type=???, pattern=???)}
coderatchet
  • 8,120
  • 17
  • 69
  • 125
  • Here a solution with an exclude based on annotation : [link](https://stackoverflow.com/a/16239076/2971820) And you can also look at this thread : [link](https://stackoverflow.com/questions/18992880/exclude-component-from-componentscan) – jpmottin Oct 12 '17 at 06:51
  • Don't use `@ComponentScan` like that, it is a test so you should be using `@ContextConfiguration` to explicitly specify what you want to load. – M. Deinum Oct 12 '17 at 09:40
  • @M.Deinum maybe, but this is an integration test and there are a lot of moving parts i need included. (at least 20 so far). It would be nice to have the filter do the work instead. – coderatchet Oct 12 '17 at 21:59
  • I nowhere said you shouldn't use `@ComponentScan` I said you shouldn't use it like that. I would suggest to exclude `@Configuration` from being component-scanned and only scan other components and explicitly `@Import` the configuration you need on a custom `@Configuration` class for the test loaded with the `@ContextConfiguration`. – M. Deinum Oct 13 '17 at 19:11

0 Answers0