java - How can I include test classes into Maven jar and execute them? -


in maven project, have test classes , source classes in same package, in different physical locations.

.../src/main/java/package/** <-- application code .../src/test/java/package/** <-- test code 

it's no problem access source classes in test classes, run test runner in main method , access alltest.class can create jar , execute tests.

 public static void main(string[] args) {     // alltest not found     result result = junitcore.runclasses(alltest.class);     (failure failure : result.getfailures()) {         system.out.println(failure.tostring());     }     system.out.println(result.wassuccessful()); } 

but doesn't work don't have access test code. don't understand since in same package.

question: how can access test classes application classes? alternatively, how can maven package fat jar including test classes , execute tests?

you should not access test classes application code, rather create main (the same main) in test scope , create additional artifact project.

however, in additional artifact (jar) need have:

  • the test classes
  • the application code classes
  • external dependencies required application code (in compile scope)
  • external dependencies required test code (in test scope)

which means fat jar addition of test classes (and dependencies). maven jar plugin , test-jar goal not suit need. maven shade plugin , shadetestjar option not neither.

so, how create in maven fat jar test classes , external dependencies?

the maven assembly plugin perfect candidate in case.

here minimal pom sample:

<project>     <modelversion>4.0.0</modelversion>     <groupid>com.sample</groupid>     <artifactid>sample-project</artifactid>     <version>1.0-snapshot</version>      <build>         <plugins>             <plugin>                 <artifactid>maven-assembly-plugin</artifactid>                 <version>2.3</version>                 <configuration>                     <descriptor>src/main/assembly/assembly.xml</descriptor>                 </configuration>                 <executions>                     <execution>                         <id>make-assembly</id>                         <phase>package</phase>                         <goals>                             <goal>single</goal>                         </goals>                         <configuration>                             <archive>                                 <manifest>                                     <mainclass>com.sample.testmain</mainclass>                                 </manifest>                             </archive>                         </configuration>                     </execution>                 </executions>             </plugin>         </plugins>     </build>      <dependencies>         <dependency>             <groupid>junit</groupid>             <artifactid>junit</artifactid>             <version>4.11</version>             <scope>test</scope>         </dependency>     </dependencies> </project> 

the configuration above setting main class defined in test classes. that's not enough.

you need create descriptor file, in src\main\assembly folder assembly.xml file following content:

<assembly     xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">     <id>fat-tests</id>     <formats>         <format>jar</format>     </formats>     <includebasedirectory>false</includebasedirectory>     <dependencysets>         <dependencyset>             <outputdirectory>/</outputdirectory>             <useprojectartifact>true</useprojectartifact>             <unpack>true</unpack>             <scope>test</scope>         </dependencyset>     </dependencysets>     <filesets>         <fileset>             <directory>${project.build.directory}/test-classes</directory>             <outputdirectory>/</outputdirectory>             <includes>                 <include>**/*.class</include>             </includes>             <usedefaultexcludes>true</usedefaultexcludes>         </fileset>     </filesets> </assembly> 

the configuration above is:

  • setting external dependencies taken test scope (which take compile scope well)
  • setting fileset include compiled test classes part of packaged fat jar
  • setting final jar fat-tests classifier (hence final file sampleproject-1.0-snapshot-fat-tests.jar).

you can invoke main following (from target folder):

java -jar sampleproject-1.0-snapshot-fat-tests.jar 

from such main, invoke of test cases following:

  • create juni test suite
  • add test suite concerned tests
  • invoke test suite plain java main

example of test suite:

import org.junit.runner.runwith; import org.junit.runners.suite; import org.junit.runners.suite.suiteclasses;  @runwith(suite.class) @suiteclasses({ apptest.class }) public class alltests {  } 

note: in case test suite concerning apptest sample test.

then have main class following:

import org.junit.internal.textlistener; import org.junit.runner.junitcore;  public class mainapptest {      public static void main(string[] args) {         system.out.println("running tests!");          junitcore engine = new junitcore();         engine.addlistener(new textlistener(system.out)); // required print reports         engine.run(alltests.class);     } } 

the main above execute test suite in chain execute of configured tests.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -