JavaRush /Java Blog /Random EN /Coffee break #87. Why is unit testing important for devel...

Coffee break #87. Why is unit testing important for developers? 5 Ways to Copy an Array in Java

Published in the Random EN group

Why is unit testing important for developers?

Source: SearchSoftwareQuality Let's discuss why unit testing is important and valuable, and how it makes debugging easier. Unit testing is a powerful tool for improving software quality. Unit tests provide fundamental verification that an application conforms to software specifications and behaves as intended. Coffee break #87.  Why is unit testing important for developers?  5 Ways to Copy an Array in Java - 1When done well, unit tests:
  • reduce the number of defects and identify them at early stages of the development life cycle;
  • improve code readability;
  • allow code reuse;
  • increase deployment speed.
Let's look at why unit testing is important, how this type of testing originated, and what the barriers to its implementation are.

History of Unit Testing

An error detected at an early stage saves time and effort. For the first 50 years of computer history, unit testing and debugging were essentially the same thing. But by the 1990s, the code had become so complex that it was often impossible to split the system into small parts to run them in isolation. In 1997, a programmer named Kent Beck created JUnit, a development environment plugin for testing small pieces of code. The test code that evaluates the source code is called unit tests. This type of unit testing has become a staple for many years. After Beck created JUnit, Martin Fowler wrote a book, Refactoring, in which he proposed ways to transform code to make it more isolated and testable. The combination of code refactoring and unit testing has led to test-driven development, where the creation of unit tests is essential to the programming process. In it, the code must be testable even before it is created. Thus, the programming process is not completed until the unit tests are run. The project can then move to the research stage at the systems or human level.

Unit Testing Example

This example demonstrates the importance of unit testing. Here JUnit evaluates a simple function that converts temperature from Fahrenheit to Celsius. Conversion formula: C = (F-32) * 5/9. Just a few lines, including the function signature and curly braces, can be implemented in code as a library function. However, it is not clear from the function that these are criteria. These options may include rounding up or down, real numbers, or upper and lower limits. Let's create example unit tests for this temperature conversion function in Perl using the Test::More module. The first line is a comment that tells the programmer what to expect from the remaining code.
# is (input, expected result, comment)
is( FtoC(32),0,'Freezing point is F32, C 0');
is( FtoC(212),100,'Boiling point is F212, C 100');
is( FtoC(59038),32767, 'Upper limit of C is 32767');
is( FtoC(59039),undefined, 'One past upper limit is error');
The JUnit framework relies on object-oriented systems and test objects, but the concept is similar.

Isolated unit tests

One of the benefits of unit tests is that they isolate a function, class, or method and test only that piece of code. Better individual components provide overall system resiliency. This way you get reliable code. Unit tests also change the nature of the debugging process. To try to fix a bug, programmers simply write a failing test and then repeat it so that it passes without violating previous expectations. This process eliminates the manual cycle of traditional debugging through setup, recreate, pause, and test. To change code to make it suitable for unit testing, programmers must change the way they work. Any pieces of code written without unit tests will likely be considered untested, at least as individual modules.

Adapting Unit Tests

Legacy software is software that has been running for a long time and was most likely written without unit tests. Legacy code has value for the company. It works stably for many years. Some programs built without unit tests process million-dollar transactions per day. But code that doesn't have unit tests turns into a big ball of dirt over time because it has been touched by many code maintenance programmers over the years. Refactoring allows programmers to gradually make changes to a system to make it testable. However, these changes take time. Several years ago, I discussed with my colleague Bob Reselman the issue of using unit tests for legacy applications. Reselman argued that implementing unit testing in applications built without it is too expensive and even stupid. Instead, he recommended that the organization start new development with unit tests and leave legacy applications alone. This may be true for COBOL, report generators, and other applications, but I would argue that in applications written in modern languages—C++, C#, Java, and Ruby—it is quite easy to retroactively add unit tests. Instead of writing them for the entire application, just add unit tests to the current change and refactor as you go.

Increased speed, quality and testability

Project managers say that planning involves trade-offs between quality, amount of work completed, resources and time. To add something to one area, you must take something away from another. Effective unit tests break this rule. This is why unit testing is important and valuable for organizations. Good unit tests produce testable code that improves quality. This code will have fewer defects, which means fewer bug fixes to complete the project faster. When software encounters bugs, unit tests speed up debugging, fixing, and writing code. And this happens in such a way that the likelihood of a defect reoccurring is significantly reduced while simultaneously improving the quality and speed of the code. While there is no magic bullet in software development, effective unit tests can speed up development, testing, and even some of the functional requirements engineering.

5 Ways to Copy an Array in Java

Source: Dev.to So you have two arrays A and B and you need to copy the elements from A to B. Well, there are different ways to do this in Java and I will show you them now.

Method one: ForLoop

This is where the good old for loop comes to our aid :
int[] A = {1,2,4,4};
int[] B = new int[];

 for (int i = 0; i < A.length; i++){
      B[i] = A[i];
}

Method two: .clone()

The array cloning method can also help achieve the desired result:
int[] A = {1,2,4,4};
int[] B = A.clone();//the clone method copies the content of A into B;

Method three: System.arraycopy()

The next way is to use the System.arraycopy() method , which is in the java.lang package . Before we move on to its use, let's discuss its signature:
public static void arraycopy(
    Object src, //:source array, in this case A
    int srcPos, //:the start index for copy, typically 0
    Object dest, //:destination object in this case B.
    int destPos, //:the index to place the copied elements
    int length //:the length of the contents to be copied
);
Application:
int[] A = {1,2,4,4};
int[] B = new int[];

System.arraycopy(A, 0, B, 0, A.length);

Method Four: Arrays.copyOf()

The next copy option we'll discuss is the Arrays class from the java.utils package . Pay attention to his signature:
public static int[] copyOf(
    int[] original, // :source array in this case A
    int newLength // :the length of the contents to be copied
);
Application:
int[] A = {1,2,4,4};
int[] B = Arrays.copyOf(A, 3);

Method five: Arrays.copyOfRange()

So, this will be the last option that we will look at in this post. It is also from the Arrays class present in the java.utils package . Let's look at his signature again:
public static int[] copyOfRange​(
    int[] original, // :source array in this case A
    int from,  //:the start index for copy, typically 0
    int to // the end index exclusive
);
Application:
int[] A = {1,2,3,4,5,6,7};
int[] B = Arrays.copyOfRange(A, 0, A.length);
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION