2017-11-27 9 views
1

수행 할 작업 : 내 응용 프로그램이 db에 올바르게 연결되는지 테스트하고 싶습니다. 그렇게하기 위해 나는이 튜토리얼을 사용했다 : http://www.baeldung.com/spring-boot-testinghttp://www.baeldung.com/spring-testing-separate-data-source 그래서 나는 그것을 저장하고 userName에 의해 발견 된 사용자가 내가 저장 한 것과 동일한지를 검사하면서 새로운 testUser를 생성 할 것이다.db와 연결 테스트, 테스트 통과하지만 db의 새 인스턴스가 생성되지 않았습니다.

작동하지 않는 항목 : 테스트가 통과해도 데이터베이스에 새 사용자가 나타나지 않습니다. 나는 이유를 모른다. 어떤 제안?

테스트 :

package com.shareabook.integration.user; 

import com.shareabook.constants.initialNumberOfPoints; 
import com.shareabook.model.Points; 
import com.shareabook.model.User; 
import com.shareabook.repository.PointsRepository; 
import com.shareabook.repository.RoleRepository; 
import com.shareabook.repository.UserRepository; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.test.context.junit4.SpringRunner; 

import java.util.Arrays; 

import static org.assertj.core.api.Assertions.assertThat; 

@RunWith(SpringRunner.class) 
@DataJpaTest 
public class userTest { 
    @Autowired 
    private UserRepository userRepository; 

    @Autowired 
    private RoleRepository roleRepository; 

    @Autowired 
    private PointsRepository pointsRepository; 

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); 

    @Test 
    public void whenFindByUserName_thenReturnUser() { 
     //given 
     User testUser = new User(); 

     // Creates new points instance 
     Points points = new Points(); 
     points.setAmountOfPoints(initialNumberOfPoints.INITIAL_NUMBER_OF_POINTS.getInitialNumberOfPoints()); 
     pointsRepository.save(points); 
     int tempId = points.getId(); 

     testUser.setUserName("userName"); 
     testUser.setFirstName("firstName"); 
     testUser.setLastName("lastName"); 
     testUser.setPassword(passwordEncoder.encode("[email protected]")); 
     testUser.setEmail("[email protected]"); 
     testUser.setIsBlocked(false); 
     testUser.setIdpoints(tempId); 
     testUser.setRoles(Arrays.asList(roleRepository.findByRole("USER"))); 

     userRepository.save(testUser); 

     //when 
     User found = userRepository.findByUserName(testUser.getUserName()); 

     //then 
     assertThat(found.getUserName()).isEqualTo(testUser.getUserName()); 


    } 
} 

------ 편집 --------- 내 테스트 조금 변경하고 지금은 예상대로 작동하고 의견 유용한 제안 후 .

package com.shareabook.integration.user; 

import com.shareabook.constants.initialNumberOfPoints; 
import com.shareabook.model.Points; 
import com.shareabook.model.User; 
import com.shareabook.repository.PointsRepository; 
import com.shareabook.repository.RoleRepository; 
import com.shareabook.repository.UserRepository; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.test.context.junit4.SpringRunner; 

import java.util.Arrays; 

import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertNotNull; 

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class userTest { 
    @Autowired 
    private UserRepository userRepository; 

    @Autowired 
    private RoleRepository roleRepository; 

    @Autowired 
    private PointsRepository pointsRepository; 

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); 

    @Test 
    public void whenFindByUserName_thenReturnUser() { 
     //given 
     User testUser = new User(); 

     // Creates new points instance 
     Points points = new Points(); 
     points.setAmountOfPoints(initialNumberOfPoints.INITIAL_NUMBER_OF_POINTS.getInitialNumberOfPoints()); 
     pointsRepository.save(points); 
     int tempId = points.getId(); 

     testUser.setUserName("userName"); 
     testUser.setFirstName("firstName"); 
     testUser.setLastName("lastName"); 
     testUser.setPassword(passwordEncoder.encode("[email protected]")); 
     testUser.setEmail("[email protected]"); 
     testUser.setIsBlocked(false); 
     testUser.setIdpoints(tempId); 
     testUser.setRoles(Arrays.asList(roleRepository.findByRole("USER"))); 

     userRepository.save(testUser); 

     //when 
     User found = userRepository.findByUserName(testUser.getUserName()); 

     //then 
     assertNotNull(found); 
     assertEquals(testUser.getUserName(), found.getUserName()); 
    } 
} 
+0

DataJpaTest 주석과 관련이 있는지 궁금합니다. 문서 (https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.html)에서 - "기본적으로 주석이 달린 테스트 @ DataJpaTest는 내장 된 메모리 내장 데이터베이스를 사용합니다 (명시 적 또는 일반적으로 자동 구성된 DataSource 대체). " 데이터베이스에서 실제 변경이 이루어질 것으로 예상되는 것처럼 들리지만 실제로 메모리 1에 대해 쿼리하고있는 것으로 보입니다. –

+0

@NickDeFazio, 고마워, 너와 SimonMartinelli가 옳았다. –

답변

2

@DataJpaTest는 메모리 내장 데이터베이스를 구성하고 @Entity 클래스를 검색하며 Spring 데이터 JPA 저장소를 구성합니다.

트랜잭션이지만 각 테스트가 끝날 때 롤백됩니다!

그래서 데이터베이스에 데이터가 없습니다.

+0

감사합니다, @SimonMartinelli. 그래서 요약 해 보겠습니다. 메모리 데이터베이스에 대한 작업을 테스트하고 있습니다. 실제로 데이터베이스에 연결되지 않았습니다. 그렇습니까? –

+0

다른 것을 구성하지 않는 한 예. –