私がやるべきこと:私のアプリケーションがdbに正しく接続しているかどうかをテストします。これを行うために私はこれらのチュートリアルを使用していました:http://www.baeldung.com/spring-boot-testinghttp://www.baeldung.com/spring-testing-separate-data-source私は新しいtestUserを作成して保存し、userNameで見つかったユーザーが保存したユーザーと等しいかどうかを確認します。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());
}
}
------ EDIT ---------私は私のテストビットを変更し、今では期待通りに働いているコメントに役立つ提案した後 。
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());
}
}
DataJpaTestアノテーションと関係があるのだろうかと思います。ドキュメント(https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.html)から - "デフォルトでは、 @ DataJpaTestは、(自動的に設定された明示的なデータソースを置き換える)内蔵メモリ内データベースを使用します。実際の変更がデータベースで行われることを期待しているように聞こえますが、実際にはメモリ1に対して照会していると思われます。 –
@NickDeFazio、ありがとう、あなたとSimonMartinelliは正しかった。 –