상세 컨텐츠

λ³Έλ¬Έ 제λͺ©

[Spring] ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±

PROGRAMMING/Backend

by koharin 2022. 2. 24. 22:17

λ³Έλ¬Έ

728x90
λ°˜μ‘ν˜•

πŸ“ ν…ŒμŠ€νŠΈ μ½”λ“œ

ν…ŒμŠ€νŠΈ μ½”λ“œλŠ” λ‹¨μœ„ κΈ°λŠ₯이 μ œλŒ€λ‘œ μž‘λ™λ˜λŠ”μ§€ ν™•μΈν•˜κΈ° μœ„ν•΄ μž‘μ„±ν•œλ‹€.

ν…ŒμŠ€νŠΈ μ½”λ“œμ—λŠ” μ‹€ν–‰ 단계, 검증 단계가 μžˆλ‹€.

μ‹€ν–‰ λ‹¨κ³„μ˜ κ²°κ³Όλ₯Ό actual μΈμžμ— λ„£κ³  비ꡐ 둜직(matcher)λ₯Ό λ„£μ–΄μ„œ 검증 λ‹¨κ³„μ—μ„œ μ‹€ν–‰ 단계가 μ˜ˆμƒν•œ λŒ€λ‘œ, κ°œλ°œμžκ°€ μƒκ°ν•œ λŒ€λ‘œ λ™μž‘ν•˜λŠ”μ§€ κ²€μ¦ν•œλ‹€. 

 

μŠ€ν”„λ§ κΈ°μ€€μœΌλ‘œ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ—¬ κ²€μ¦ν•˜λŠ” 과정을 μž‘μ„±ν•΄λ³΄μ•˜λ‹€.

 

 

πŸ“ Dependency μΆ”κ°€ (Maven)

		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.13.2</version>
			<scope>test</scope>
		</dependency>   
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework-version}</version>
			<scope>test</scope>
		</dependency>     
		<dependency>
			<groupId>de.flapdoodle.embed</groupId>
			<artifactId>de.flapdoodle.embed.mongo</artifactId>
			<version>3.3.0</version>
			<scope>test</scope>
		</dependency>
  • pom.xml νŒŒμΌμ— μœ„μ˜ maven dependencyλ₯Ό μΆ”κ°€ν•΄μ„œ λ‹€μš΄λ‘œλ“œν•œλ‹€. 

 

 

πŸ“ λ‹¨μœ„ ν…ŒμŠ€νŠΈ 클래슀 생성

  • μŠ€ν”„λ§ ν”„λ‘œμ νŠΈμ— test 폴더가 μžˆλŠ”λ°, test 폴더 κ΅¬μ‘°λŠ” 메인 νŒ¨ν‚€μ§€ ꡬ쑰와 λ™μΌν•˜λ‹€.
  • ν”„λ‘œμ νŠΈ νŒ¨ν‚€μ§€ ν•˜μœ„μ— test 클래슀 νŒŒμΌμ„ μƒμ„±ν•œλ‹€. 파일λͺ…은 ν…ŒμŠ€νŠΈ μ½”λ“œμž„μ„ λͺ…μ‹œν•˜κΈ° μœ„ν•΄ Test둜 λλ‚˜λŠ” κ²ƒμœΌλ‘œ ν†΅μΌν–ˆλ‹€.

 

 

 

πŸ“ UserRepository ν…ŒμŠ€νŠΈμ½”λ“œ μž‘μ„±

@Repository
public interface UserRepository extends MongoRepository<User, String>, CrudRepository<User, String>{
	
	Optional<User> findByNickname(String nickname);
	Optional<User> findByIdAndPassword(String id, String password);
}
  • 메인 νŒ¨ν‚€μ§€μ— User entity에 λŒ€ν•œ MongoDB 쿼리 μž‘μ—…μ„ μ§„ν–‰ν•˜λŠ”λ° ν•„μš”ν•œ μΈν„°νŽ˜μ΄μŠ€μΈ UserRepositoryλ₯Ό λ§Œλ“€μ—ˆλ‹€.
  • UserRepositoryλ₯Ό μ΄μš©ν•΄μ„œ User collection λŒ€μƒμœΌλ‘œ MongoRepository, CrudRepositoryλ₯Ό 상속받아 ν•΄λ‹Ή λ ˆν¬μ§€ν† λ¦¬ λ‚΄ 쿼리λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€. (findById, insert, save, count ... λ“±)
  • UserRepositoryμ—μ„œ λ³„λ„μ˜ λ©”μ†Œλ“œλ₯Ό λ§Œλ“  μ΄μœ λŠ” λ‹€λ₯Έ ν•„λ“œλ“€μ„ ν™œμš©ν•΄μ„œ MongoDB에 쿼리λ₯Ό μ‹€ν–‰ν•΄μ„œ μ›ν•˜λŠ” User 객체λ₯Ό μ–»κ³  μ‹ΆκΈ° λ•Œλ¬Έμ΄λ‹€.
  • λ”°λΌμ„œ 이 μ‚¬μš©μž μ •μ˜ λ©”μ†Œλ“œλ“€μ΄ κ°œλ°œμžκ°€ μ›ν•˜λŠ” λŒ€λ‘œ λ™μž‘ν•˜λŠ”μ§€ ν™•μΈν•˜κΈ° μœ„ν•΄ UserRepository λ©”μ†Œλ“œ λŒ€μƒμœΌλ‘œ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ³΄μ•˜λ‹€.

 

package org.smu.blood;

import static org.junit.Assert.assertEquals;

import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.smu.blood.database.User;
import org.smu.blood.database.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@EnableMongoRepositories
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
@Rollback(value = true)
public class UserRepositoryTest {
	@Autowired
	private UserRepository repository;
	
	@Test
	public void existsByNicknameTest() {
		User createUser = repository.insert(new User("asdf@naver.com", "qwe123", "asdf", 1, false));
		Optional<User> result = repository.findByNickname(createUser.getNickname());
		assertEquals(result.equals(Optional.empty()), false);
	}
	
}
  • @EnableMongoRepositories
    • MongoRepositoryλ₯Ό μƒμ†λ°›λŠ” UserRepositoryλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•œ annotation
  • @RunWith(SpringJUnit4ClassRunner.class)
    • ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό JUnit4μ—μ„œ 싀행함을 λͺ…μ‹œν•˜κΈ° μœ„ν•œ annotation
  • @ContextConfiguration
    • ν…ŒμŠ€νŠΈ μ½”λ“œ μ‹€ν–‰ μœ„ν•΄ ν•„μš”ν•œ 정보λ₯Ό 얻을 수 μžˆλŠ” servlet xml configuration 파일 경둜 λͺ…μ‹œν•˜κΈ° μœ„ν•œ annotation.
    • test와 λ‹€λ₯Έ νŒ¨ν‚€μ§€μ— μžˆλŠ” νŒŒμΌμ„ μ‚¬μš©ν•˜λ―€λ‘œ xml configuration 파일 λͺ…μ‹œκ°€ ν•„μš”ν•˜λ‹€.
    • 기타 ν•„μš”ν•œ μ˜΅μ…˜λ“€λ„ λͺ…μ‹œ κ°€λŠ₯ν•˜λ‹€. 
  • @Rollback
    • ν…ŒμŠ€νŠΈ μ½”λ“œ μ‹€ν–‰ μ‹œ MongoDB에 영ν–₯을 쀄텐데, κ²°κ³Όλ₯Ό μœ μ§€ν• μ§€ λ˜λŒλ¦΄μ§€ λͺ…μ‹œν•˜λŠ” annotation.
      • true인 경우 λ‚΄λΆ€ MongoDBμ—μ„œ ν…ŒμŠ€νŠΈ μ§„ν–‰ν•˜κ³  λ‘€λ°±ν•œλ‹€. (운영용 DBμ—λŠ” 생성 X)
  • @Test
    • ν…ŒμŠ€νŠΈ λ©”μ†Œλ“œμž„μ„ ν‘œμ‹œν•˜κΈ° μœ„ν•œ annotation
  • User 객체λ₯Ό μƒμ„±ν•΄μ„œ User collection에 documentλ₯Ό μƒμ„±ν•˜κ³ , UserRepository의 findByNickname λ©”μ†Œλ“œλ‘œ μœ μ €λ₯Ό μ°ΎλŠ” 쿼리λ₯Ό μ‹€ν–‰ν•œλ‹€. 
  • assertEquals(a, b)
    • a와 bκ°€ λ™μΌν•œμ§€ 확인

 

 

 

πŸ“  νšŒμ›κ°€μž… ν…ŒμŠ€νŠΈμ½”λ“œ μž‘μ„±

package org.smu.blood;

import static org.junit.Assert.*;
import java.util.Optional;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.smu.blood.database.User;
import org.smu.blood.database.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@EnableMongoRepositories
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
@Rollback(value = true)
public class SignUpTest{
	@Autowired
	private UserRepository repository;
	
	//private static final Logger logger = LoggerFactory.getLogger(SignUpTest.class);
	
	@Test
	@DisplayName("Same id user test")
	public void CheckSameId() {
		repository.insert(new User("koharin@naver.com", "qwe123", "koharin", 1, false));
		
		User newUser = new User("koharin@naver.com", "qwerty", "asdf", 2, true);
		Optional<User> userById = repository.findById(newUser.getId());
		assertEquals(userById.equals(Optional.empty()), false);
		
	}
	@Test
	@DisplayName("Same nickname user test")
	public void CheckSameNickname() {
		repository.insert(new User("qwe@naver.com", "qwe123", "qwe", 1, false));
		
		User newUser = new User("asdf@naver.com", "qwerty", "qwe", 2, true);
		Optional<User> userByNickname = repository.findByNickname(newUser.getNickname());
		assertEquals(userByNickname.equals(Optional.empty()), false);
	}
	
}
  • ν•΄λ‹Ή λ©”μ†Œλ“œμ—μ„œλ„ UserRepository λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. 
  • CheckSameIdλŠ” 이미 μžˆλŠ” 아이디인지 μ²΄ν¬ν•˜λŠ” λ©”μ†Œλ“œμ΄κ³ , CheckSameNickname은 이미 μžˆλŠ” λ‹‰λ„€μž„μΈμ§€ ν™•μΈν•˜λŠ” λ©”μ†Œλ“œμ΄λ‹€.
  • 두 λ©”μ†Œλ“œ λ‘˜λ‹€ User document ν•˜λ‚˜λ₯Ό λ§Œλ“€μ–΄λ†“κ³ , μƒˆλ‘œ 계정을 λ§Œλ“œλ €λŠ” μ‚¬μš©μžμ˜ User 객체의 아이디 및 λ‹‰λ„€μž„μ„ 톡해 findById 및 findByNickname λ©”μ†Œλ“œλ‘œ 쿼리λ₯Ό μ‹€ν–‰ν•΄μ„œ Optional<User> 객체λ₯Ό μ–»λŠ”λ‹€.
  • λ§Œμ•½ 쀑볡이 μ—†λ‹€λ©΄ Optional.empty와 동일할 κ²ƒμ΄λ―€λ‘œ assertEquals둜 얻은 객체가 Optional.Empty()와 μΌμΉ˜ν•˜λŠ”μ§€ ν™•μΈν•œλ‹€.
  • 이 μ½”λ“œμ—μ„œλ„ UserRepositoryλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄ @EnabledMongoRepositories annotation을 μΆ”κ°€ν–ˆλ‹€.

 

 

 

πŸ“ ν…ŒμŠ€νŠΈ μ½”λ“œ μ‹€ν–‰

  • Junit을 톡해 μ‹€ν–‰ν•  수 μžˆλ‹€.
  • eclipse IDEλ₯Ό μ‚¬μš©ν•˜μ˜€κ³ , 였λ₯˜ 없이 정상 μ‹€ν–‰ μ‹œ λͺ¨λ‘ μ΄ˆλ‘μƒ‰ 체크 ν‘œμ‹œκ°€ 되며, 였λ₯˜κ°€ μžˆλŠ” 경우 였λ₯˜ λ©”μ‹œμ§€λ₯Ό 보여쀀닀.

 

 

728x90
λ°˜μ‘ν˜•

κ΄€λ ¨κΈ€ 더보기