Skip to content

Instantly share code, notes, and snippets.

@kodiyan
Last active August 29, 2015 14:04
Show Gist options
  • Save kodiyan/bcb459acb2b7519ef602 to your computer and use it in GitHub Desktop.
Save kodiyan/bcb459acb2b7519ef602 to your computer and use it in GitHub Desktop.
Run Junit Test from Maven and Main Class
Project Structure:
TestDemo
|-src/main/java
|--com.main
|---Cal.Java
|-src/test/java
|--com.test
|---CalcAdd.java
|---CalTUnit.java
|---RunTest.java
|-pom.xml
###########################
package com.main;
public class Calc {
public static int addNumbers(int a, int b) {
return a+b;
}
public static void main(String[] args) {
System.out.println("I am in main method");
}
}
###########################
package com.test;
import org.junit.runner.JUnitCore;
public class CalcAdd {
static int sysNumber;
public static void addNumbers (int a, int b){
}
public static void callJunit () {
sysNumber = Integer.parseInt(System.getProperty("number"));
System.out.println("Printing dNumber " +sysNumber);
for (int i=0; i<sysNumber; i++) {
JUnitCore junit = new JUnitCore();
junit.run(CalcTUnit.class);
}
}
}
###########################
package com.test;
import org.junit.Before;
import org.junit.Test;
public class CalcTUnit {
@Before
public void before() {
System.out.println("@ Before in CalcTest");
}
@Test
public void test() {
System.out.println("------------ Number Printed from System property ---------------- = " +CalcAdd.sysNumber);
}
}
###########################
package com.test;
import static org.junit.Assert.*;
import org.junit.Test;
public class RunTest {
@Test
public void test() throws ClassNotFoundException {
System.out.println("--------------------- Test Started ------------------------------");
CalcAdd.callJunit();
}
}
###########################pom.xml######################
<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>Demo</groupId>
<artifactId>TestDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- This plug in to execute main java class -->
<!-- <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.main.Calc</mainClass>
</configuration>
</execution>
</executions>
</plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<inherited>true</inherited>
<configuration>
<systemProperties>
<property>
<name>number</name>
<value>${number}</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment