TestNG

What is TestNG?

TestNG is framework of unit test.

How to use TestNG?

1. If you use maven to build your project, add the following lib dependencies(TestNG, Spring-test) in pom.xml file.



     org.springframework 
     spring-test 
    
     ${org.springframework.version} 


     org.testng 
     testng 
     ${testng.version} 

2. Create your test class.

//To start Spring.
@Contextconfiguration(location={"/applicationContext.xml"})
public class UserServiceTest extends AbstracttestNGSpringContextTests {
    @Autowired   //To inject Spring bean of UserService in this bean.
    private UserService userService;
    
    @Test   //To mark method need to be tested.
    public void hasMatchUser(){
        boolean b1 = userService.hasMachUser("admin", "123456");
        boolean b2 = userService.hasMatchUser("admin", "22222");
        assertTrue(b1);
        assertTrue(!b2);
    }

    @Test
    public void findUserByUserName(){
        User user = userService.findUserByUserName("admin");
        assertEquals(user.getUserName(), "admin");
    }

    @Test
    public void loginSuccess() {
        User user = userService.findUserByUserName("admin");
        user.setUserId(1);
        user.setUserName("admin");
        user.setLastIp("192.168.12.7");
        user.setLastVisit(new Date());
        userService.loginSuccess(user);
    }
 
}

3. Run your test.
Run your test file only.

[Note: What I do is only for study. To make me and you master it efficiently.]

你可能感兴趣的:(TestNG)