[junit] junit 사용하기
junit을 java에서 사용하기 위해서는 2가지 라이브러리가 필요하다.
junit.jar와 hamcrest-core.jar가 필요하다. 다운은 https://github.com/junit-team/junit/wiki/Download-and-Install 여기서 받을 수 있고,
junit 공식 사이트는 http://junit.org/
User
package com.slipp.user;
import com.slipp.PasswordMismatchException;
import com.slipp.db.Database;
public class User {
private String userid;
private String password;
private String name;
private String email;
public User(String userid, String password, String name, String email) {
super();
this.userid = userid;
this.password = password;
this.name = name;
this.email = email;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean matchPassword(String newPassword) {
return this.password.equals(newPassword);
}
public static boolean login(String userid, String password) throws NotFoundUserException, PasswordMismatchException {
User user =Database.findByUserId(userid);
if(user==null){
throw new NotFoundUserException();
}
if(!user.matchPassword(password)){
throw new PasswordMismatchException();
}
return true;
}
@Override
public String toString() {
return "User [userid=" + userid + ", password=" + password + ", name=" + name + ", email=" + email + "]";
}
}
Database
package com.slipp.db;
import java.util.HashMap;
import java.util.Map;
import com.slipp.user.User;
public class Database {
private static Map<String, User> users=new HashMap<>();
public static void addUser(User user){
System.out.println("user : " +user.toString());
users.put(user.getUserid(), user);
}
public static User findByUserId(String userid) {
return users.get(userid);
}
}
DatabaseTest
package com.slipp.db;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import com.slipp.user.User;
public class DatabaseTest {
@Test
public void addAdnFind(){
User user =new User("userid", "password", "name", "javagigi@slipp.net");
Database.addUser(user);
User dbUser =Database.findByUserId(user.getUserid());
assertEquals(user, dbUser);
}
@Test
public void addAndFindWhenNotExisted(){
User dbUser=Database.findByUserId("userI2");
assertNull(dbUser);
}
}
UserTest
package com.slipp.user;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.slipp.PasswordMismatchException;
import com.slipp.db.Database;
public class UserTest {
public static User TEST_USER =new User("userid", "password", "name", "java@rok.net" );
@Test
public void matchPassword(){
assertTrue( TEST_USER.matchPassword("password"));
}
@Test
public void notMatchPassword(){
assertFalse(TEST_USER.matchPassword("password2"));
}
//로그인 성공 여부 테스트
@Test
public void login() throws Exception {
User user=UserTest.TEST_USER;
Database.addUser(user);
assertTrue(User.login(TEST_USER.getUserid(), TEST_USER.getPassword()));
}
@Test(expected=NotFoundUserException.class)
public void loginWhenNotExitedUser() throws Exception {
User.login("userId2", TEST_USER.getPassword());
}
@Test(expected=PasswordMismatchException.class)
public void loginWhenPasswordMissmatch() throws Exception {
User user =UserTest.TEST_USER;
Database.addUser(user);
User.login("userid", "password2");
}
}
NotFoundUserException
package com.slipp.user;
public class NotFoundUserException extends Exception {
}
PasswordMismatchException
package com.slipp;
public class PasswordMismatchException extends Exception{
}
login_action.jsp
<%@page import="com.slipp.user.User"%>
<%@page import="com.slipp.db.Database"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String userId=request.getParameter("userId");
String password=request.getParameter("password");
User user=Database.findByUserId(userId);
if(user==null){
//사용자가 존재하지 않는다는 것을 에러 메시지 전송
}
if(password.equals(user.getPassword())){
//로그인 처리
}
%>
댓글 ( 4)
댓글 남기기