How to run UnitTests in maven which is in src/test-integration/java folder

后端 未结 2 1015
清酒与你
清酒与你 2020-12-14 08:12

When we say mvn test, usual way is that maven will look for the tests present in src/test/java folder. But I have my tests in some different folder, namely src/integration-t

2条回答
  •  北海茫月
    2020-12-14 08:19

    @khmarbaise is right with his recommendation (so +1 for that) but I want to answer your question without speculating about the reasons why the test source are located somewhere else.

    If your tests are located in another directory than the standard src/test/java directory, the most simple solution is to change the default value of the testSourceDirectory configuration parameter which is defined in the Super POM.

    e.g. for src/foobar/java use

    
      src/foobar/java
    
    

    then you can simply run mvn test to execute the tests.


    More complex solution...

    If you do not want to change the pom.xml configuration you can specifiy the testSourceDirectory parameter on the command line like this:

    mvn -DtestSourceDirectory=src/foobar/java clean test
    

    But be sure that your sources are compiled. Otherwise they will not be found and executed. In the above example the test sources are not placed at a location that gets compiled by default, so we nevertheless have to change the pom and add the directory to the list of test sources by using the buildhelper plugin:

    
        org.codehaus.mojo
        build-helper-maven-plugin
        1.8
        
            
                add-test-source
                generate-sources
                
                    add-test-source
                
                
                    
                        src/foobar/java
                    
                
            
        
    
    

    If you do not want to change the configuration of the default value in the pom and not want to pass the new directory at the commandline you have to configure the path in the maven-buildhelper-plugin and the maven-surefire-plugin in your pom.xml like this:

    
        
            
                org.codehaus.mojo
                build-helper-maven-plugin
                1.8
                
                    
                        add-test-source
                        generate-sources
                        
                            add-test-source
                        
                        
                            
                                src/foobar/java
                            
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.14.1
                
                    src/foobar/java
                
            
        
    
    

    Now again the simple usage of mvn test will execute the test at the not standard location.

提交回复
热议问题