1
@Service

public class MyRegistry {

private final Map<String, CustomClass> services;

@Autowired
public MyRegistry(List<CustomClass> msgList) {
 this.services = msgList.stream().collect(Collectors.tounmodifiableMap(CustomClass:: getId, s->s));
}

public CustomClass getIns(final String name){
 final CustomClass customClass = services.get(name);


 return customClass;
}

I have been asked to test this class by loading spring context, I have tried running the below test case with @ActiveProfiles("local") that works but that fails on dev environment, how to fix this issue.

Below is my test case:

@SpringBootTest
@ActiveProfiles("local")
class MyTestClass {
    @Autowired
    MyRegistry myRegistry;

   @ParameterizedTest
   @CsvSource( value={constant1, constant2} )
   void getValue(String id){
# assertNotNull(myRegistry.getInstanceId());
}
}

Looking for solution.

NewDevs
  • 11
  • 1
  • Hi & Welcome to [so]! :) How to fix? (too few infos!) but some direction: in your local environment this (integration!) test succeeds, due to the conditions given by `@ActiveProfiles("local")` and `constant1, constant2` and your "local environment" ... these conditions are not met on "dev"! You have to "detect" this difference (error messages will help), and (smartly) clear... – xerx593 Mar 22 '23 at 09:18
  • On the other hand: "I have been asked to test this class by loading spring context" can (light-minded) also be interpreted as "to wirte a *unit test*" ((by->) still/also loading the spring context), then you are good off with: "mocks" ! (don't need to care about "environment specifics", but just test the consistency and correctness of "this class", at the price of "mocking external 'responses/messages'") – xerx593 Mar 22 '23 at 09:22

0 Answers0