This is not the first time I have heard of unit testing, but I have just heard that I have never used it. How to test a module? Do I have to write a test program specifically for a unit and then use the test unit code to test? I thought so. After learning the spring framework, I realized what unit testing is all about.
The first instance injected by set in the previous article is the test object. Perform unit testing.
1, copy the jar package
junit-3.8.2.jar (4.x mainly adds annotation applications)
2, write business class
public class User{ privateString username; publicString getUsername() { returnusername; } publicvoid setUsername(String username) { this.username= username; } //添加方法 publicString login() throws Exception{ if("admin".equals(username){ return"success"; }else{ return"error"; } } }
3, Define the test class
It is best to create a separate project for the test class, or define a separate folder for storage. You need to inherit junit.framework.TestCase
4 and add a test method
The test method must be public , there should be no return value. The method name must start with test and have no parameters.
The test method has an order of execution, according to the order of the method definition.
Multiple test methods test the same business method. Generally, each Every logical branch structure has been tested.
public class TestUserextends TestCase{ publicvoid testUser_Success() throws Exception{ //准备数据 Useraction = new User(); action.setUsername("admin"); //调用被测试方法 Stringresult = action.login(); //判断测试是否通过 assertEquals("success",result); } }
Run the program. If the test is successful, the result as shown below will appear.
If the operation fails and there is a method that fails the test, it will be displayed. Which method is wrong? The green bar in the image above will turn red.
5, the life cycle method of the test class
//用来进行初始化操作 @Override protectedvoid setUp() throws Exception { System.out.println("setUp..."); } //用来做销毁操作 @Override protectedvoid tearDown() throws Exception { System.out.println("tearDown..."); }
setUp method will be executed once before each test method. The tearDown method will be executed once after each test method
The above is the content of spring framework learning (3) junit unit test. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"

Platformindependenceallowsprogramstorunonanyplatformwithoutmodification,whilecross-platformdevelopmentrequiressomeplatform-specificadjustments.Platformindependence,exemplifiedbyJava,enablesuniversalexecutionbutmaycompromiseperformance.Cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages such as Python and JavaScript to enhance cross-language interoperability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!
