0

I need to pass String value (jbehave story file name) to .java file from cmd. Something like this:

mvn verify -PMyTests -DstoryName=purchasing.story

To use in java in that way:

System.out.println(storyName);

My setup:

Now tests being launched by

mvn verify -PMyTests

pom.xml file structure:

...
<dependencies>
    ...
</dependencies>
<build>
    ...
<build>

<profiles>
    <profile>
        <id>MyTests</id>
        <build>
            <plugins>
                ...
            </plugins>
        </build>
    </profile>

MyStories.java:

public class MyStories extends JUnitStories {
    ...
    @Override
    protected List<String> storyPaths() {
        return new StoryFinder()
            .findPaths(codeLocationFromClass(embeddableClass).getFile(), asList(
                    "**/purchasing.story"
                    ), null);
    }
}

Tried these topics: Is there a way to use maven property in java class during compilation, How to pass java code a parameter from maven for testing, but with no result. Straightforward tutorials, search tips or documentation with examples appreciated.

P.S. Found solution which is simple enough and works for me: http://syntx.co/languages-frameworks/how-to-pass-parameters-to-the-junit-tests-from-the-maven-surefire-plugin/

Solution provided in comments (topic which I "duplicated") is more complex than this one so it is easier for novice to make mistake implementing it

Community
  • 1
  • 1
Karloss
  • 817
  • 3
  • 9
  • 27
  • You need to include your Java code as well. – mikołak Sep 17 '13 at 08:15
  • see http://stackoverflow.com/questions/13245411/pass-a-java-parameter-from-maven-for-testng – krishnakumarp Sep 17 '13 at 08:19
  • 1
    I'm still not quite sure, or convinced that this is a duplicate. The link you've pointed to shows how to run a different group of tests via a test provider in surefire. How is this exactly related? Please, kindly explain, as I fail to understand how five people voted for "close as duplicate". Perhaps I'm not seeing something. – carlspring Sep 18 '13 at 09:37
  • I think you are right. I used these original topic before creating my own (unsuccessfully) as I wasn't experienced enough to fit it for my needs. Also original topic uses annotations and imports in java and also it has more complicated pom.xml which I don't need. – Karloss Sep 18 '13 at 11:56
  • Sure it was my mistake as I wasn't clear enough in my topic/topics title – Karloss Sep 18 '13 at 12:03

1 Answers1

0

You need to read the System property like this:

public class MyStories extends JUnitStories {
    ...
    @Override
    protected List<String> storyPaths() {
        String storyName = System.getProperty("storyName");
        return new StoryFinder()
            .findPaths(codeLocationFromClass(embeddableClass).getFile(),
                       asList("**/" + storyName),
                       null);
    }
}
carlspring
  • 31,231
  • 29
  • 115
  • 197