Java JUnit testing and TDD

JUnit

What is JUnit?

JUnit is a unit testing framework for the Java language. It was founded by Kent Beck and Erich Gamma, has gradually become due sUnit Kent Beck's xUnit family of one of the most successful. JUnit JUnit extension has its own ecosystem. Most Java development environment have been integrated as a tool for JUnit unit testing.

Simply put, is used for testing, the method of determining the result of the implementation is correct, an alternative is to simplify testing.

The traditional test code you write so maybe

public class T {
	
	public int add(int x, int y) {
		return x + y;
	}
	
	public static void main(String[] args) {
		System.out.println(new T().add(3, 8));
	}
}

Then click Run to view the results of console output of the console, is not it, and you expect the same results.

Of course, this way can, but he has a lot of drawbacks: the trouble to write, can not be tested at the same time more methods, manual checking.

To solve this problem occurs only the unit test, of course, the code is not written by testers, developers should need to know.

JUnit benefits

1. A series of test methods can be written, for all items, or interface unit test method. 
2. After the start, automated testing, and the results of determination, without human intervention. 
3. Only need to see the final result, the interface method to know whether the entire project open. .
4. Each independent cell test, started by Junit, automatically invoked. No additional call statement.
5. Add, delete, shielding test method, does not affect other test methods. There are open-source framework appropriate support for JUnit.

Let's look at an example: 

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.test.junit.T;

public class TTest {
	@Test
	public void test() {
		int z = new T().add(5, 3);
		assertEquals(8, z);
	}

}

I believe we all can understand this example, assertEquals method is used to predict the target value is not the expected value. When you select this method, in Eclipse, right Run As -> JUnit Test, then you will see the results.

It found that the emergence of a green strip means the test passed, to prove that you write the code are correct.

When you change the value of assertEquals 9, and then run, you'll find it red.

It is clear that there is no meaning to pass the test, Errors (error program itself) is 0, failures (test failure) is 1, expected to be 9, but is 8.

So the word has been spread

keeps the bar green ,to keeps the code clean

Keep this strip of green, indicating that the code is normal

JUnit also provides some Annotation

  1. @Test: Test Methods
    1. (expected=XXException.class)
    2. (timeout=xxx)
  2. @Ignore: test methods overlooked
  3. @Before: run before each test method
  4. @After: run after each test method
  5. @BeforeClass: All test run before the start
  6. @AfterClass: running after the end of all tests

TDD

TDD is test-driven development (Test-Driven Development), it is also a methodology for agile development. Before TDD is re-develop the code, write your unit test cases, test code is determined by what code to write. It's the whole idea is to drive the progress of the entire software development through testing, of course, this is a higher requirements and standards for testers.

Three principles of TDD

  • Before you write good unit tests fail, do not write any production code.
  • As long as there is a unit test fails, do not write test code. It is not a failure by the compiler.
  • Product code just to make the current failed unit tests successfully, but do not write.

TDD need to go through a lot of practice, and on the quality of the people involved very demanding, but in this fast-paced changing the giant Internet products, projects and more focus on business processes, so that only relatively fine team only use this kind of development model.

Published 39 original articles · won praise 20 · views 10000 +

Guess you like

Origin blog.csdn.net/cong____cong/article/details/104313320