Tag Archives: eclipse

JUnit 5 : Environment Setup


Let us discuss on JUnit 5 environment setup in Java Application.

Pre-Requisite

JUnit 5 does not required any major setup. It’s required following library and IDE:

  • JDK : Min 1.8 or above
  • IDE : Eclipse/STS or Netbean or IntelliJ
  • Latest version of Junit 5 is readily available with latest version of IDE

More :

Junit 5 Setup in Java Application

You can add JUnit 5 library in your Java Application by follow below steps:

  • Right clock on your project -> Properties->Build Path-> Configure Build Path
  • Select Junit Library by following below steps in screen shot.
Junit 5 Environment Setup
  • After clicking in Next button (Step 5). You will get below screen where you can select Junit 5.
JUnit 5 Environment Setup
  • After click on Finish button (Step 7) this JUnit 5 library will add on your application.
  • Now you can click on “Apply” or “Apply and Close” button to complete this process.

Now your application is setup to write test JUnit 5 test cases.

In further topic you will learn about how to write JUnit Test Cases.

Junit: Jacoco Code Coverage Plugin Integration with Eclipse/STS


In previous post you learn about the Junit Code Coverage Report generation by using Maven POM.xml that’s help to generate report for auditing purpose and check the status of junit test case implementation but everytime for these report you have to visit directory target/site/jacoco/index.html or target/jacoco-report/index.html

To reduce this effort, we can add this Jacoco plugin our eclipse/STS IDE and directly check the status over IDE.

Integration of JACOCO Code Coverage Plugin in Eclipse/STS

You can follow these steps to integrate this JACOCO code coverage plug in you eclipse or STS IDE.

  • Go to IDE top header Help->Eclipse Market Place it will open the pop up for Eclipse Market Place.
  • Type text as “Code Coverage” in search text box and click “Go” button. You will get below screen
JACOCO Code Coverage Plugin for Eclipse/STS
  • You will get list of plugins select the plugin “EclEmma Code Coverage” click on Install button for this plugin.
  • You will get next screen where need to accept terms and conditions and click continue.
  • After installing this code coverage plugin, It will ask to restart your IDE.
  • After restart your IDE, go to the top Windows->Show View->Others then search for “Coverage” as below then select option click on Open button.
  • You will see one additional tab “Coverage” on your console view section as below.
Coverage tab on view
  • Also get “Coverage As” option as below when right click on your application. Now you can run you Junit.
JACOCO coverage as option
  • Once you run your application Junit by using “Coverage As” option you will see the results on “Coverage” tab as below.
JUnit Code coverage report by Eclipse/STS plugin

To learn more detail about this report and metrics header you can follow this link Junit: Code coverage by using JaCoCo.

Conclusion

In this topic you learn about the steps to add code coverage plugin in you eclipse/STS IDE.

If this port help you to add Code Coverage plugin in you eclipse/STS IDE the like this post and share your comments. In case you are facing issue related to code coverage implementation share with us will try to resolve it.

How to Enable YAML Editor in Eclipse?


Below are steps to Enable YAML/YML editor on eclipse IDE.

  • Open Eclipse IDE
  • Go to Help menu -> Eclipse Marketplace.
  • Search for term “YAML”
  • You will get lots of option select “YAML Editor” and click on install as in below screen.

YAML Editor in Eclipse
Enable YAML/YML Editor on Eclipse

  • Click check box corresponding to “Accept Terms and Conditions”.
  • Click on Apply button and restart eclipse.

More

To know more about YAML Syntax, Configuration with Java and other supporting language, frameworks and tools, Sample configuration files and JSON and YAML conversion follow below YAML Tutorials and YAML related exceptions follow YAML Issues.

 

How to make and auto run /executable jar with dependencies?


To make auto run/executable jar by eclipse follow below steps.

  • Create Java Project in Eclipse by following these steps as below New -> Project ->Java Project -> Click Next
  • Type Project Name as JavaAutoRunExample -> click on Finish button.
  • Expend project JavaAutoRunExample on left panel and create package as com.facingissuesonit.test
  • Create below class TestHelloWorld inside package com.facingissuesonit.test
    package com.facingissuesonit.test;
    
    public class TestHelloWorld {
    
    	public static void main(String[] args) {
    		System.out.println("Inside Package facing Issues on IT");
    		System.out.println("Test Hello World !!");
    	}
    
    }
    

Now your Project Structure will looks like as below.

Auto Run Jar Project

Now in below steps will show  to convert this project as auto run/executable jar where entry point would be main method of class TestHelloWorld.

Note : If in your project having multiple classes having main method you can decide one entry point by below steps.

  • Build and Test you project so that there would be any compile and runtime issues. Select class TestHelloWord -> Right click -> Run As -> Java Application. You will get below console output.
    Inside Package facing Issues on IT
    Test Hello World !!
    
  • Right Click on JavaAutoRunExample ->export ->Java->Runnable Jar File -> Click on Next.

Auto run Jar Selection

  • Select Launch Configuration as TestHelloWorld and export destination where want to keep this auto run/executable jar and dependency jar options as below.Auto Run lanunch Selection
  • Now your auto run/executable jar is ready . For executing it go to command promt (CMD) and select location for file and use below command

java -jar AutoRunTest.jar

C:\Users>cd C:\Users\facingissuesonit\Documents

C:\Users\facingissuesonit\Documents>java -jar AutoRunTest.jar
Inside Package facing Issues on IT
Test Hello World !!

 

 

Hope this blog was helpful for you.

Leave you feedback to enhance more on this topic so that make it more helpful for others.

How to create Maven Java Console Project?


In this below example will show you create maven java project from console and import to eclipse.

Maven provide maven-archetype-quickstart archetype artifact to create console based java project from command prompt.

Pre-Requisite

Go to directory/ workspace where you want to create maven Java project. Below is command to create it.

mvn archetype:generate -DgroupId=com.{group}.app -DartifactId={App Name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Example :

mvn archetype:generate -DgroupId=com.facingissuesonit.app -DartifactId=test-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Because we are creating our first project with maven that’s why it will download lots of jars and dependency from maven repository.

C:\Users\facingissuesonit\workspace-sample>mvn archetype:generate -DgroupId=com.facingissuesonit.app -DartifactId=test-app -DarchetypeArtifactId=maven-archetype
-quickstart -DinteractiveMode=false

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\facingissuesonit\workspace-sample
[INFO] Parameter: package, Value: com.facingissuesonit.app
[INFO] Parameter: groupId, Value: com.facingissuesonit.app
[INFO] Parameter: artifactId, Value: test-app
[INFO] Parameter: packageName, Value: com.facingissuesonit.app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\facingissuesonit\workspace-sample\test-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.133 s
[INFO] Finished at: 2017-06-06T03:55:59-04:00
[INFO] Final Memory: 14M/148M
[INFO] ------------------------------------------------------------------------

Above example command will create project with name test-app in your current directory and will show directory structure as below.

Test-app
-src
--main
---java
----com
-----facingissuesonit
------app
-------App.java
--test
---java
----com
-----facingissuesonit
------app
-------AppTest.java
-pom.xml

Project pom.xml will have entry as below.


<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.facingissuesonit.app</groupId>
  <artifactId>test-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Build Project : Go to project directory and run below command to build your project. It will go through all these steps to complete build life cycle as you can see from console logs.

1. validate
2. generate-sources
3. process-sources
4. generate-resources
5. process-resources
6. compile
7. test

mvn package

C:\Users\facingissuesonit\workspace-sample\test-app>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\facingissuesonit\workspace-sample\test-app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\facingissuesonit\workspace-sample\test-app\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test-app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\facingissuesonit\workspace-sample\test-app\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\facingissuesonit\workspace-sample\test-app\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test-app ---
[INFO] Surefire report directory: C:\Users\facingissuesonit\workspace-sample\test-app\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.facingissuesonit.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test-app ---
[INFO] Building jar: C:\Users\facingissuesonit\workspace-sample\test-app\target\test-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.372 s
[INFO] Finished at: 2017-06-06T04:07:45-04:00
[INFO] Final Memory: 16M/145M
[INFO] ------------------------------------------------------------------------

Now our build is successful. We can use below command in target folder to run test-app project.

C:\Users\facingissuesonit\workspace-sample\test-app\target>java -cp test-app-1.0-SNAPSHOT.jar com.facingissuesonit.app.App
Hello World!

Import to Eclipse : To import this project in eclipse first we need to convert this project according to eclipse and then import it.

  • Convert to Eclipse Maven Project : Use below command
    mvn eclipse:eclipse
C:\Users\facingissuesonit\workspace-sample\test-app>mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ test-app >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ test-app <<< [INFO] [INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ test-app --- [INFO] Using Eclipse Workspace: C:\Users\facingissuesonit\workspace-sample [WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:\Program Files\Java\jre1.8.0_73 [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER [INFO] Not writing settings - defaults suffice [INFO] Wrote Eclipse project for "test-app" to C:\Users\facingissuesonit\workspace-sample\test-app. [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.717 s [INFO] Finished at: 2017-06-06T04:56:14-04:00 [INFO] Final Memory: 12M/114M [INFO] ------------------------------------------------------------------------ 

Import in Eclipse : Follow below steps to import test-app project in Eclipse

Eclipse File -> Import -> General-> Existing Project in Workspace -> Select Project test-app

Maven Java Console Project