0

I'm trying to apply TDD in my workflow when I'm working on Spring 5 (not Spring Boot) application, but my running tests take about 45s, which makes my development cycle quite slow. Standard unit tests are super quick, but I have one test which validates Spring context configuration - it takes over 30s on its own.

My idea is to disable this particular test every time during the development cycle and run it just from time to time - e.g. when I create a Docker image with my application/make a commit/push with changes.

So here is what I come up with so far:

  1. Define property when docker-profile is run:

    <profiles>
      <profile>
        <id>docker-build</id>
        <properties>
            <build.profile.id>docker-build</build.profile.id>
        </properties>
        <build>...</build>
      </profile>
    </profiles>
    
  2. Use it in test.properties:

    app.profile=${build.profile.id}
    
  3. Use @EnabledIf annotation in test:

     @TestPropertySource("classpath:test.properties")
     @SpringJUnitConfig( classes = MainConfiguration.class )
     @EnabledIf("#{'${app.profile}' == 'docker-build'}")
     public class SpringConfigTest {
       ...
     }
    

And it doesn't seem to work - I can be wrong, but app.profile property doesn't seem to be available when I call it in @EnabledIf. Or maybe I took a wrong route and there is simpler way to disable this particular test - I run it as mvn test at the moment. I would like to know why app.profile property isn't visible for @EnabledIf though.

Edit I've discovered I can skip the first two steps and define property in command line:

mvn -Dapp.profile=docker-build test

But not sure why I can't get property from test.properties file

Krzysztof Skrzynecki
  • 2,345
  • 27
  • 39

1 Answers1

0

I believe you need to load the context in this case for the spring version of @EnabledIf to eval the expression. So it would be:

@EnabledIf(expression = "#{'${app.profile}' == 'docker-build'}", loadContext = true)

Also make sure the property file is included in maven testResources with filtering enabled:

    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    </build>

I think above should resolve the specific property issue you are facing. But since this does not skip loading the spring context perhaps a better way is to configure in the maven profile to skip test based on package or test class name - similar to Is there a way to tell surefire to skip tests in a certain package?

camtastic
  • 955
  • 6
  • 15