junit 3テスト

4208 ワード



package com.zyl.service;

public class UsersDAO {
	
	public String saveUser(String user){
		
		System.out.println(" ");
		
		return user;
	}
	
	public String findUserById(Long id){
		
		String users = "";
		if(id.intValue()==1)
		  users = "zyl";
		
		return users.toLowerCase();
		
	}

}
-------------------------------------
package com.zyl.service;

public class LoginService {
    private String username;
    private String password;
    
    
	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String login(){
		
		if("admin".equals(username)&&"admin".equals(password))
			return "admin";
			else
				return "input";
		
		
		
	}
	
}
----------------------------------
package com.zyl.service.test;

import junit.framework.TestCase;

import com.zyl.service.LoginService;

public class LoginServiceTestCase extends TestCase {
    private LoginService loginService;
    
	protected void setUp() throws Exception {
		loginService = new LoginService();
	}

	protected void tearDown() throws Exception {
		loginService = null;
		System.gc();
	}

	public void testLogin() {
		
		loginService.setUsername(null);
		loginService.setPassword("admin");
		
		//this.assertEquals(" admin ","admin", loginService.login());
		this.assertEquals(" input ","input",loginService.login());
		
	}
	public static void main(String[] args) {
		
		
	}
	

}
----------------------------------------
package com.zyl.service.test;

import junit.framework.TestCase;

import com.zyl.service.UsersDAO;

public class UserDAOTestCase extends TestCase {

	private UsersDAO usersDao;
	
	public UserDAOTestCase() {
		// TODO Auto-generated constructor stub
	}
	
	public void setUp(){
		
		usersDao = new UsersDAO();
		System.out.println(" UsersDAO");
		
	}
	
	public void tearDown(){
		
		usersDao=null;
		System.gc();
		System.out.println(" UsersDAO , ");
	}
	
	
	
	public void testSaveUser(){
		
		
		
		//String user="zyl";
		String user=null;
		this.assertNotSame(" ",user,usersDao.saveUser(user));
	
		
	}
	
	public void testFindUserById(){
		
		
		
		String userFound = 
		usersDao.findUserById(2L);
		
		assertEquals(" 1 ","zyl",userFound);
		
		
		
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//Baseline Text
		//junit.textui.TestRunner.run(UserDAOTestCase.class);
		junit.swingui.TestRunner.run(UserDAOTestCase.class);
		
		
		
		

	}

}
--------------------------------
package com.zyl.service.test;

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

	public static Test suite() {
		TestSuite suite = new TestSuite("Test for com.zyl.service.test");
		//$JUnit-BEGIN$
		suite.addTestSuite(UserDAOTestCase.class);
		suite.addTestSuite(LoginServiceTestCase.class);
		//$JUnit-END$
		return suite;
	}

}