Running an intersection of tests using Junit and Maven
Asked Answered
D

1

7

I have a number of tests identified using the Spring @IfProfileValue flag

@IfProfileValue{"a", "c"}
@Test
public void testA{ Do Stuff }

@IfProfileValue{"a", "b"}
@Test
public void testB{ Do Stuff }

@IfProfileValue{"a", "b"}
@Test
public void testC{ Do Stuff }

@IfProfileValue{"b"}
@Test
public void testD{ Do Stuff }

I can run all the tests using

mvm clean install -Dtest-group=a -Dtest-group=b

I want to run only the tests that match @IfProfileValue={"a","b") (Test B/C) so is there a way to run only an intersection of these two values using maven?

Disputatious answered 26/3, 2015 at 15:1 Comment(0)
T
1

Edit: You can annotate the class with @ProfileValueSourceConfiguration@ and provide your own implementation ofProfileValueSource`, as described in this answer.

Looks like is not possible with Maven alone. It looks like it can build array from multiple arguments with same name:

mvn test -Dtest-group=a -Dtest-group=c

will ran test annotated with @IfProfileValue(name = "test-group", values = {"c"}). Neither comma notation will work, it will treat 'a,c' as literal:

    mvn test -Dtest-group=a,c
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.s17t</groupId>
<artifactId>showcase</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.1.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compier-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Java code:

package showcase;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
@TestExecutionListeners
public class SimpleTest {

    @Configuration
    static class ContextConfiguration {

    }   

    @Test
    @IfProfileValue(name = "test-group", values = {"a", "b"})
    public void testPhoneLogIsReadable() {
        System.out.println("I'm a and b");
        assertTrue("Phone log is not readable.", true);
    }

    @Test
    @IfProfileValue(name = "test-group", values = {"c"})
    public void testPhoneLogHasRecords() {
        System.out.println("I'm c");
        assertFalse("Phone log does not have records.", false);
    }   

}
Tisbe answered 2/4, 2015 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.