BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

JUnit 4 Tutorial: Maven Java 7 - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




Note

In this tutorial, we will learn how to implement and execute unit testing, using the JUnit Framework and Maven.





What is Maven?

Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software:

  • It describes how software is built.
  • It describes its dependencies.

Contrary to preceding tools like Apache Ant, Maven uses conventions for the build procedure, and only exceptions need to be written down.

The answer to "What is Maven?" depends on whom we we ask:

  1. Majority of Maven users are going to call Maven a "build tool" that is used to build deployable artifacts from source code. A build tool such as Ant is focused solely on preprocessing, compilation, packaging, testing, and distribution.
  2. Build engineers and projects managers may consider Maven as something more comprehensive: a "project management tool". A project management tool such as Maven provides not only features found in a build tool but also features of running reports, generating a web site, executing unit testing and much more. The concept of testing is built right into the Maven lifecycle.



Eclipse & Maven

Click on the Help menu and then click on Install New Software...

Help_Install_New_Software.png

In the next window, click on the Maven Integration for Eclipse check-box.

AvailableSoftwareMaven.png

Then click on Next.

InstallDetailsMaven.png

Finish and our Eclipse may need to be restarted.





Creating Maven project

Click on the File => New => Other... menu, select Maven Project option and click on the Next button:

FIle_New_Other_Maven_Project.png

Click Next => Next:

MavenArcheTypeQuickStart.png

We need to type the Group Id, Artifact Id and Package as in the following screen and click on Finish.

GroupArtifactIds.png

Eclipse gives us this:

PackageExplorerMavenJUnit.png

We want to change the JRE System Library used by Maven, from version 1.5 to version 1.7 by adding the following lines to our pom.xml file:

<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>com.bogotobogo.examples</groupId>
  <artifactId>junitmavenexample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>junitmavenexample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Then, right click on the name of the Project, click on the Maven menu option and then click on the Update Project... menu option:

Project_Maven_UpdateProject.png

In the next window, just click on the OK button, then we can see the Project JRE has changed to version 1.7:

NewPackageExplorerMavenJUnit.png

Finally, because we want to use JUnit 4, we need to modify the version of the JUnit library by modifying our pom.xml file and change the version from 3.8.11 to 4.11:

<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>com.bogotobogo.examples</groupId>
  <artifactId>junitmavenexample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>junitmavenexample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
      </plugin>
    </plugins>
  </build>
</project>


Here is how our file structure looks like:

scr_test_code_in_PackageExplorer.png



Source code

Concatenation.java

package com.bogotobogo.examples.junitmavenexample;

public class Concatenation {
    public String concat(String s1, String s2) {
	return s1 + s2;
    }
}


ConcatenationTest.java

package com.bogotobogo.examples.junitmavenexample;
import static org.junit.Assert.*;

import org.junit.Test;

public class ConcatenationTest {
	
    @Test
    public void testConcat() {
	Concatenation myConcat = new Concatenation();
        String result = myConcat.concat("Hello", "World");
        assertEquals("HelloWorld", result);
    }
	
}




JUnit test run

To execute our tests, we can right click on the name of our class (ConcatenationTest.java), then click on Run As and finally click on JUnit Test:

JUnitTestRun.png

This figure indicates we pass the JUnit test!

SuccessfulJUnitTestRun.png

Project file is available here: junitmavenexample.tar.gz





JUnit & Maven Tutorial

  • JUnit 4 Introduction (Hello World)
  • JUnit 4 Test with Eclipse Luna (Hello World)
  • JUnit 4 Test with Maven (Hello World)








  • Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

    YouTubeMy YouTube channel

    Sponsor Open Source development activities and free contents for everyone.

    Thank you.

    - K Hong







    JUnit & Maven Tutorial



    JUnit 4 Introduction (Hello World)

    JUnit 4 Test with Eclipse Luna (Hello World)

    JUnit 4 Test with Maven (Hello World)




    Sponsor Open Source development activities and free contents for everyone.

    Thank you.

    - K Hong







    Scala Play!



    Scala/Java Play app with Angular





    Spring Boot



    Spring Boot : Hello world with Mavan 3

    Spring Boot : Hello world with Gradle 2

    Spring Boot (Gradle 2) : Hello world with Authentication

    Spring Boot : Deploying War file to Tomcat 8's webapps

    How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

    Maven : mvn command cheat sheet

    Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

    Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

    Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

    Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)





    Java Tutorials



    Java Tutorial Home

    Basics - Compiling and Launching

    Inner Classes

    Constructor

    Enums

    Static & Finally

    Default and Protected

    Polymorphism

    Exception Handling

    Exception Handling II

    String Class

    Threads

    Threads II - State Transition

    Threads III - Synchronization

    Object Class

    File I/O

    Serialization

    ArrayList

    Autoboxing

    Java Graphics Interface I - Basics

    Java Graphics Interface II - Labels, Text Fields, Layouts

    Java Graphics Interface III - paintComponent

    TCP Sockets Server/Client

    Scala - Functional Java Programming

    Apache CXF install

    Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

    What is Apache Maven?

    Maven life cycle

    Eclipse Maven 3 plugin on Ubuntu 14.04

    Apache Maven 3 - Setting up and creating a project

    Apache Maven 3 - Compile, build, and install a Maven project

    Apache Maven 3 - Dependencies

    Apache Maven 3 - Web Application

    Apache Maven 3 - Plugins (compiler)

    Apache Maven 3 - Plugins (Jetty)

    Eclipse CDT / JNI (Java Native Interface) / MinGW



    Spring Framework

    Hello World App with Spring 4 & Maven 3 - Part I











    Contact

    BogoToBogo
    contactus@bogotobogo.com

    Follow Bogotobogo

    About Us

    contactus@bogotobogo.com

    YouTubeMy YouTube channel
    Pacific Ave, San Francisco, CA 94115

    Pacific Ave, San Francisco, CA 94115

    Copyright © 2024, bogotobogo
    Design: Web Master