1

Hello I have a springboot app and there are two possible spring profiles, one environment config related (dev,prod,staging...) and one with mocked some remote service, let's call it remoteServiceMock. So I have origin service that marked with :

@Profile("!remoteServiceMock")

And mock bean:

@Profile("remoteServiceMock")

Everything, except test are fine , when I run application with :

install -Dspring.profiles.active=dev,remoteServiceMock

or

install -Dspring.profiles.active=dev

So corresponding beans are loaded. But I got problems with testing. My test class:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@Category({IntegrationTest.class})
@TestPropertySource("classpath:test.properties")
@IfProfileValue(name = "spring.profiles.active",
        values = "remoteServiceMock")
public class DeltaServiceMockTest {

    @Autowired
    private RemoteService remoteService;

    @Test
    public void shouldIntiService() throws Exception {
        assertNotNull(remoteService);
    }

    @Test
    public void shouldGetMockDelta() throws Exception {
        RemoteData remoteDate = remoteService.getData(new Date(), new Date());
        assertEquals(15,remoteDate.getActivity().size());
    }
}

The problem that tests are being executed only if spring.profiles.active exactly match . So tests will run only if I will write:

@IfProfileValue(name = "spring.profiles.active", = "dev,remoteServiceMock")

The question are:

1) Is it possible to write some kind of extension for IfProfileValue with "contains"/"not contains" profile feature

2) Is the any other better solution for my goal ? (So I want to mock one (in feature there are several will be) of remote service and deploy my app to staging env).

Thanks

Maksym
  • 4,434
  • 4
  • 27
  • 46

1 Answers1

2

You may want to look into @ActiveProfiles feature and customize it based on ActiveProfilesResolver (take a look at the bottom of this Spring docs section) .

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • Seems I was too sleepy and didn't find easy solution for skipping, like here (http://stackoverflow.com/a/32892291/3502543), but ActiveProfileResolver also fine for me :) Thanks a lot. – Maksym Dec 02 '15 at 07:19