Practice of object-oriented design based on Java

Keywords: Java Junit Maven

Object oriented design practice (based on java)

caiyi 2021/10/1


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

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


Junit test


Common math


pinyin4j Hanzi to Pinyin


Maven error reporting solution

Add the following configuration statement to pom.xml


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


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

Create 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;
			return false;

Create 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 {
	public void test() {
		assertEquals(true,new LeapYear().isLeapYear(2020));
		assertEquals(false,new LeapYear().isLeapYear(2019));

junit unit test, right-click → 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