Practice of object-oriented design based on Java

Keywords: Java Junit Maven

Object oriented design practice (based on java)

caiyi 2021/10/1

source: https://www.icourse163.org/spoc/course/ECNU-1464731161

Chapter I Maven

Maven concept

Automatically download and manage jar packages, configure build path, and have local build tools to manage, compile, test, run, package and publish java projects

Maven compilation workflow

Maven directory structure

Maven project construction process

First create the maven project

Select maven project

Check create a simple project

Enter group id and artifact id

Then search the name of the toolkit in the mvn central warehouse

https://mvnrepository.com/

Select the appropriate version and copy the dependent text

Add the dependent text to the project pom.xml

Maven compile and run

Right click Project → Run As → Maven Build

Enter clean package → Apply → Run in Goals

Compilation succeeded

During compilation, he will automatically download the jar package

Run program

Summarize and build the functions of the tool

  • Automatically help programmers identify and download third-party libraries (jar s)

  • Complete the compilation of the whole project (call javac.exe)

  • Complete the whole project unit test process (call JUnit tool)

  • Complete the project packaging (jar/war and other formats, call jar.exe)

Some commonly used dependent text

opencc4j simplified to traditional

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>opencc4j</artifactId>
    <version>1.7.1</version>
</dependency>

Junit test

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>

Common math

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.6.1</version>
</dependency>

pinyin4j Hanzi to Pinyin

<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.1</version>
</dependency> 

Maven error reporting solution

Add the following configuration statement to pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Chapter 2 unit test and Junit

software test

The process of operating the program under specified conditions to find program errors, measure software quality, and evaluate whether it can meet the design requirements

Software test classification

Unit vs integration test

White box vs black box test

Automatic vs manual test

Regression test, stress test

JUnit: a java language unit testing framework. Most Java ides integrate JUnit as a unit testing tool

JUnit usage

Create a new maven project using the methods in the previous chapter

⭐ Remember to import the dependent text of JUnit in pom.xml, otherwise an error will be reported

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
    </dependency>
</dependencies>

Observe the project directory. We put business code in src/main/java and test code in src/test/java

Create LeapYear.java in src/main/java. Given a year, judge whether it is a leap year

public class LeapYear {
    
	public boolean isLeapYear(int year) {
		if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
			return true;
		}
		else{
			return false;
		}
	}
}

Create LeapYearTest.java in src/test/java

Add @ Test annotation to the header of each Test method, so that JUnit will automatically execute these Test methods

Import is to import a class or several classes, and import static is to import all static methods of a class, so that we don't need to write Assert.assertEquals() when the program is called

import static org.junit.Assert.*;
import org.junit.Test;

public class LeapYearTest {
	
	@Test
	public void test() {
		assertEquals(true,new LeapYear().isLeapYear(2020));
		assertEquals(false,new LeapYear().isLeapYear(2019));
	}
}

junit unit test, right-click LeapYearTest.java → Run As → JUnit Test

The result is correct!

Modify the code and run the wrong result! Not what we expected

We can also run JUnit programs through Maven Test

Run error

Change the false in the eighth line to true to run correctly

The difference between the two

JUnit can only execute one test class at a time, and Maven can execute it in batch

Posted by bob_dole on Fri, 01 Oct 2021 14:53:38 -0700