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.
<!-- lib dependency for test-->
<dependency>
<groupId> org.springframework </groupId>
<artifactId> spring-test </artifactId>
<!--The version value is referred from <properties> </properties> -->
<version> ${org.springframework.version} </version>
</dependency>
<dependency>
<groupId> org.testng </groupId>
<artifactId> testng </artifactId>
<version> ${testng.version} </version>
</dependency>
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.]