0

Trying to make mocked Spring Context for unit tests purpose. Our Spring Configuration is pretty big and as for now trying to get this thing using Spring + Springockito Annotations.

Problem that I've stumbled upon is that I'd like to have multiple Java Classes taking care of the Context Creation/Mocking. As for now this looks like this (let's say Class a takes class B and List of ClassCs as constructor arguments):

//declaring context classes below
@ContextConfiguration(loader = SpringockitoAnnotatedContextLoader.class,
      classes = { 
          ClassA.class,
          ClassB.class,
          SubClassCOne.class,
          SubClassCTwo.class,
          ... //list goes on and on with more mocks
      }
)
public class Configurator {
   @Autowired
   ClassA classA;

   @ReplaceWithMock
   ClassB classB;

   @Autowired
   List<ClassC> classesC;

   @Autowired
   SubClassCOne subclassCOne;

   ....

Problem is that List of C subclasses is far bigger than I'd like to have here (not to mention I'm putting whole responsibility on just one class), so I thought of another Class that will take care of creations like this:

ListProvider.java:

//THIS ANNOTATION WON'T BE TAKEN INTO ACCOUNT
@ContextConfiguration(loader = SpringockitoAnnotatedContextLoader.class,
      classes = {
          SubClassCOne.class,
          SubClassCTwo.class,
          ...
      }
)
public class ListProvider {
   @Autowired
   List<ClassC> classesC;

   ...
   public List<ClassC> getClassesC(){
       return classesC;
   }
   ...

Configurator.java:

@ContextConfiguration(loader = SpringockitoAnnotatedContextLoader.class,
      classes = {
          ClassA.class,
          ClassB.class,
          ListProvider.class,
          ...
      }
)
public class Configurator {
   @Autowired
   ClassA classA;

   @ReplaceWithMock
   ClassB classB;

   @Autowired
   ListProvider listProvider

   @Autowired //probably not autowired anymore?
   List<ClassC> classesC;

   ...
   //then somewhere it'll take classesC from listProvider using getter?

But then the problem comes that even though ListProvider will be autowired correctly, it won't make use of @ContextConfiguration classes and it won't find any ClassC subclasses unless I won't provide everything in Configurator.java, which is exactly what I'd like to avoid.

Is there a change to split this Context Configuration up into multiple files?

GoMati
  • 67
  • 2
  • 12

1 Answers1

0

Separate out your config into default and test using profiles:

@Configuration
@ComponentScan(basePackages = "com.greg")
public class MainConfig {
}

@Configuration
@Profile("test")
public class TestConfig {
}

Then run your test with a test profile

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=MainConfig .class)
@ActiveProfiles("test")
public class ATest {

So the test will pick up MainConfig at all time but will only pick up TestConfig when the profile is set to test. The @Profile can also be used to distinguish which beans are loaded on a scan.

Essex Boy
  • 7,565
  • 2
  • 21
  • 24