This is the 15th day of my participation in Gwen Challenge

As a development, how can daily programming without self-testing, today I will introduce the unit test fighter – JUnit framework simple application.

Introduce a,

JUnit is a unit testing framework for the Java language. Often used for white-box testing, regression testing, and unit testing during development.

JUnit GitHub address: github.com/junit-team

Here is a brief introduction to the application of JUnit in the SpringBoot framework.

  • Org. Springframework. Boot 2.1.4. RELEASECopy the code
  • Junit: 4.12Copy the code
  • Gradle

  • The IDEA of 2018.2

Detect JUnit dependencies

Spring Boot projects already have JUnit framework support by default, which can be viewed in build.gradle:

Dependencies {compile "junit:junit:4.12"}Copy the code
Three, basic use

The simple test code is as follows:

@RunWith(SpringRunner.class) @SpringBootTest public class OneTest { @Test public void doTest() { int num = new Integer(1); Assert.assertEquals(num, 1); }}Copy the code

3.1 Notes

Comments list

  • @runwith: The runtime environment identified as JUnit, allowing JUnit to run the Spring test environment and get the support of the Spring context
  • @SpringBoottest: Gets the startup class, loads the configuration, determines the loading method for the Spring program, and goes back to find the main configuration startup class (annotated by @SpringBootApplication)
  • @test: declare the method to be tested;
  • @beforeClass: Execute only once for all tests and must be static void.
  • AfterClass: Execute only once for all tests and must be static void;
  • @before: method executed Before each test method;
  • @after: method executed before each test method;
  • @ignore: ignores the method and does not execute it at startup;

3.2 Timeout Test

Set the timeout attribute to Test in milliseconds:

@Test(timeout = 1000)

3.3 Assertion Testing

Assertion tests, also known as expected tests, are at the heart of unit testing and are expressions that determine test results. Assert methods in Assert objects:

  • Assert.assertEquals contrasts two values equal
  • Assert.assertnotequals contrasts two values that are not equal
  • AssertSame Compares references to two objects to be equal
  • Assert.assertArrayEquals compares two arrays to be equal
  • Assert.asserttrue Verifies that the return is true
  • Assert.assertflase verifies that the return is false
  • Assert. Null assertNull validation
  • Assert.assertNotNull verifies non-NULL

A code example is as follows:

@Test public void doTest() { String[] string1 = {"1", "2"}; String[] string2 = string1; String[] string3 = {"1", "2"}; Assert.assertEquals(string1, string2); Assert.assertEquals(string2, string3); Assert.assertSame(string1, string2); Assert.assertSame(string2, string3); String2, string3, different references}Copy the code

3.4 Database Test

When testing data insertion, editing, and so on, there are ways to drop data that you don’t want to modify. Simply add “@Transactional” to the test class so that the data does not persist and taint the database.

Example code is as follows:

@Test 
@Transactional 
public void saveTest() 
{    
User user = new User();    
user.setName("Tom");    
user.setAge(20);    
user.setPwd("666666");    
userRepository.save(user);    
System.out.println("userId:" + user.getId());    
Assert.assertTrue(user.getId()>0); 
}
Copy the code

The id is found in the execution result and the test passes, indicating that the data is added is normal, but it is found that there is no such data in the database.

If you remove “@Transactional”, the database will insert normally.

After watching it for so long, I don’t like it. Why don’t I feel sorry for you?