Search code examples
javajunitazure-functions

@ParameterizedTest not working with parameters


I'm creating test cases for an Azure Function. I'd like to use parameterized tests but it refuses to pick up any test that has parameters. I used the official Azure Functions Maven archetype to generate the project. Here's the relevant parts from the POM (NOTE: I put in the exclusion to make sure I wasn't accidentally loading in junit4 (didn't change anything), and there is no specific surefire-plugin configuration section in my POM).

        <dependency>
            <groupId>com.microsoft.azure.functions</groupId>
            <artifactId>azure-functions-java-library</artifactId>
            <version>2.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <version>1.18.0</version>
                <configuration>
                    <appName>${functionAppName}</appName>
                    <resourceGroup>example</resourceGroup>
                    <appServicePlanName>example</appServicePlanName>
                    <region>example</region>
                    <!-- <pricingTier></pricingTier> -->
                    <!-- <disableAppInsights></disableAppInsights> -->
                    <runtime>
                        <!-- runtime os, could be windows, linux or docker-->
                        <os>linux</os>
                        <javaVersion>11</javaVersion>
                    </runtime>
                    <appSettings>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>~4</value>
                        </property>
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

and here's the test code:

public class FunctionTest {
    @Test
    public void testTrue() {
        assertTrue(true);
    }

    @ParameterizedTest
    @ValueSource(strings = {"example"})
    public void testNoParams() {
        assertTrue(true);
    }

    @ParameterizedTest
    @ValueSource(strings = {"example"})
    public void testOneParam(String string) {
        assertEquals(string, "example");
    }

    @ParameterizedTest
    @MethodSource
    public void testExtractSiteKeyShouldReturnMatchingGroup(String input, String output) throws Exception {
        Function function = new Function();
        assertEquals(function.extractSiteKey(Logger.getGlobal(), input), output);
    }

    private static Stream<Arguments> testExtractSiteKeyShouldReturnMatchingGroup() {
        return Stream.of(
            Arguments.of(null, null),
            Arguments.of("", null),
            Arguments.of(" ", null),
            Arguments.of("notmatching", null),
        );
    }
}

The only two tests that are picked up are, which are the two that don't have parameters:

  <testcase classname="com.example.FunctionTest" name="com.example.FunctionTest.testNoParams" time="0.001"/>
  <testcase classname="com.example.FunctionTest" name="com.example.FunctionTest.testTrue" time="0"/>

As some additional information, it also refuses to pick up the test cases that aren't prefixed with "test", e.g. testOneParam works, oneParam doesn't.


Solution

  • The issue was fixed by explicitly adding in the maven-surefire plugin version above 2.22.2:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M6</version>
    </plugin>
    

    The one provided out of the box is version 2.12.4 (apparently it only picks up @ParameterTest annotations as of junit 5.5.1 and surefire-plugin version 2.22.2)