New Java Movement: Test Driven Development 3---User Registration 2
Test-driven development (TDD) is actually not a new concept, but the essence of this methodology is often misunderstood. In my case, I think test-driven development means writing unit tests to fully test the code. In fact, this is one of the most common misunderstandings about test-driven development. Test-driven development not only emphasizes testing, but also emphasizes drive development. The purpose of writing test cases is not only testing, but also a design, a design of functions and interfaces, and a design from the perspective of users of functions or interfaces, while the design in other methodologies is Design from a developer's perspective, so test-driven development is more conducive to producing good designs.
But just designing through testing and running tests to ensure code quality is not enough to produce high-quality software products. Test-driven development also emphasizes refactoring, which is to refactor the unreasonable parts of the code, and this refactoring needs to be carried out during every test writing, development, and verification process. Since there are test cases to guarantee it, we can be confident and bold Refactor without worrying about unintended consequences.
Back to our project, in MainServlet, we implemented all business logic, but we will soon realize that if all application logic is written into this class, that class will become If it becomes a giant, it will eventually become unmaintainable, so we need to put the user registration business logic implementation into the user module. As you can see, we have begun to reconstruct our system.
First, we create the user package and create the UserMngr class under this package because we estimate that in addition to maintaining basic user information, the user module also needs to maintain user groups, user levels, user points and other information. It is not necessary for the caller to fully understand these details, so we use the Facade mode here, and all operations on the user are performed through UserMngr. The code is as follows:
public class UserMngr { public static long registerUser(Map<String, Object> userInfo) { return 101; } }
We see that in this class, we only return a value and do nothing, in order to quickly verify that our reconstructed architecture can work and the specific functions can To be added later. In addition, you need to pay attention to the parameter items. The Map object is used here, that is, the parameters are passed in an array similar to PHP. It is more common in Java to design a value object class for this purpose, but this often results in defining many value object classes, or due to the need for sharing, a value object is very large. Here, Map is used to pass parameters, which eliminates the need for such trouble.
Add the code to call the user module in MainServlet below:
private void registerUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String userName = null; if (request.getParameter("userName") != null) { userName = request.getParameter("userName"); } Map<String, Object> userInfo = new HashMap<String, Object>(); userInfo.put("userName", userName); long userId = UserMngr.registerUser(userInfo); Map<String, Object> model = new HashMap<String, Object>(); model.put("userId", "" + userId); request.setAttribute("model", model); /*this.getServletContext().getRequestDispatcher("/caporder/apply_capital.jsp") .forward(request, response);*/ }
Run the test case below and it should pass. This proves that my reconstruction of the architecture is successful this time.
The above is the content of the New Java Movement: Test Driven Development 3---User Registration 2. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



DeepSeek's official website is now launching multiple discount activities to provide users with a shopping experience. New users sign up to get a $10 coupon, and enjoy a 15% limited time discount for the entire audience. Recommend friends can also earn rewards, and you can accumulate points for redemption of gifts when shopping. The event deadlines are different. For details, please visit the DeepSeek official website for inquiries.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Gate.io Sesame Open is the world's leading blockchain digital asset trading platform, including fiat currency trading, currency trading, leveraged trading, perpetual contracts, ETF leveraged tokens, wealth management, Startup initial public offering and other sections, providing users with security, stability, openness and transparency.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java Made Simple: A Beginner's Guide to Programming Power Introduction Java is a powerful programming language used in everything from mobile applications to enterprise-level systems. For beginners, Java's syntax is simple and easy to understand, making it an ideal choice for learning programming. Basic Syntax Java uses a class-based object-oriented programming paradigm. Classes are templates that organize related data and behavior together. Here is a simple Java class example: publicclassPerson{privateStringname;privateintage;

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

Gate.io Exchange is one of the world's leading cryptocurrency trading platforms. This guide provides step-by-step tutorials to help users register and trade with Gate.io. The registration process includes selecting the registration method (phone, email or social account), filling in information, setting a login password, and completing identity authentication. Trading tutorials include accessing trading pages, selecting trading pairs, entering trading information, placing an order, and viewing order status. With the guidance of this article, users can easily start trading cryptocurrency on Gate.io.
