简体   繁体   English

使用DataJpaTest Spring Boot 2.1.0更新失败

[英]Failed to update using DataJpaTest spring boot 2.1.0

Using unit test for a spring boot parent 2.1.0. 对Spring Boot父版本2.1.0使用单元测试。 I need to test the update of values after applying update to a List object inside Game object. 将更新应用于Game对象内的List对象后,我需要测试值的更新。 I read in another thread to add the following statement but could't use in my case 我读了另一个线程以添加以下语句,但在我的情况下无法使用

  @Modifying(clearAutomatically = true) 

GameRepositoryTest.java GameRepositoryTest.java

import com.game.kalah.domain.Game;
import static com.game.kalah.utils.Constants.*;
import static com.game.kalah.utils.Status.*;
import java.util.Arrays;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
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.test.context.junit4.SpringRunner;
//import static org.assertj.core.api.Assertions.*;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;

/**
 *
 * @author Nesrin
 */
@RunWith(SpringRunner.class)
// DataJpaTest supports rollback after running every test case
@DataJpaTest
@ActiveProfiles("test")

public class GameRepositoryTest {

         private Integer[] INITIAL_BOARD;
         private Game testGame;
         private static Game palyedGame;

         @Autowired
         TestEntityManager em;

         @Autowired
         private GameRepository repository;

         @Before
         public void setup() {
                  INITIAL_BOARD = new Integer[]{6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 0};
                  testGame = new Game();
                  testGame.setBoardList(Arrays.asList(INITIAL_BOARD));
                  testGame.setStatus(PLAYER1TURN);
                  //  testGame.setId(anyInt());
                  testGame.setMessage(START_MESSAGE);
                  testGame.setId(1);
                  fisrtPitPlayerOneMove();
         }

         @Test
         public void test_save_game() {
                  // 1- Insert new game
                  Game newGame = repository.save(
                          new Game(START_MESSAGE)
                  );
                  // 2- Check saved game data for reporting
                  Optional<Game> retrieved = repository.findById(1);
                  Game savedGame = retrieved.get();

                  // playing with test options :)
                  /*
                  * return to a good reference 
                  * https://objectpartners.com/2013/09/18/the-benefits-of-using-assertthat-over-other-assert-methods-in-unit-tests/
                   */
                  //import static org.junit.Assert.*;
                  assertNotNull(newGame);
                  assertThat(newGame, isA(Game.class));
                  assertThat(savedGame, instanceOf(Game.class));
                  assertThat(testGame, is(newGame));
                  assertThat(testGame, equalTo(savedGame));
                  assertEquals(testGame, savedGame);
                  assertNotSame("Not Same", retrieved, testGame);
                  assertThat(testGame, sameInstance(testGame));
                  assertTrue(savedGame.equals(testGame));

                  //import static  org.assertj.core.api.Assertions.*;
//                  assertThat(repository.findAll()).containsExactly(newGame);
                  savedGame.setBoardList(Arrays.asList(new Integer[]{0, 7, 7, 7, 7, 7, 1, 6, 6, 6, 6, 6, 6, 0}));
                  // 3- apply change on the saved game then save again to chek update effect
                  Game afterFirstMove = repository.save(savedGame);
                  assertNotNull(afterFirstMove);
                  assertThat(afterFirstMove, isA(Game.class));
                  assertEquals(savedGame, afterFirstMove);

         }


         private static void fisrtPitPlayerOneMove() {
                  palyedGame = new Game();
                  palyedGame.setId(1);
                  palyedGame.setMessage(IN_PROGRESS_MESSAGE + palyedGame.getId());
                  palyedGame.setStatus(PLAYER1TURN);
                  palyedGame.setBoardList(Arrays.asList(new Integer[]{0, 7, 7, 7, 7, 7, 1, 6, 6, 6, 6, 6, 6, 0}));

         }
}

Game.java Game.java

@Data
@Entity
public class Game {

         @Id
         @GeneratedValue(strategy = GenerationType.AUTO)
         private Integer id;
         @ElementCollection
         @Column(name = "pits")
         private List<Integer> boardList;
         private Status status;
         private String message;

         public Game() {
         }

         public Game(String message) {
                  this.boardList = Arrays.asList(new Integer[]{6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 0});
                  this.status = Status.PLAYER1TURN;
                  this.message = message;
         }



}

GameRepository.java GameRepository.java

import com.game.kalah.domain.Game;
import org.springframework.data.repository.CrudRepository;

/**
 *
 * @author Nesrin
 */
public interface GameRepository extends CrudRepository<Game, Integer> {
}

The error is as follows, which is at the line of calling save for the second time in the test class 错误如下,它是在测试类中第二次调用save的行

 Game afterFirstMove = repository.save(savedGame);

test_save_game(com.game.kalah.repository.GameRepositoryTest) Time elapsed: 0.106 s <<< ERROR! test_save_game(com.game.kalah.repository.GameRepositoryTest)经过的时间:0.106 s <<<错误! java.lang.UnsupportedOperationException at com.game.kalah.repository.GameRepositoryTest.test_save_game(GameRepositoryTest.java:85) com.game.kalah.repository.GameRepositoryTest.test_save_game的java.lang.UnsupportedOperationException(GameRepositoryTest.java:85)

pom.xml parent part pom.xml父部分

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

Did you try use mutable list? 您是否尝试过使用可变列表? Arrays.asList(array) creates an immutable list. Arrays.asList(array)创建一个不可变的列表。

Try to use new ArrayList<>(Arrays.asList(array)) . 尝试使用new ArrayList<>(Arrays.asList(array)) check this link 检查此链接

Also, it would be helpful if you post your whole error logs. 另外,如果您发布整个错误日志,也会很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 更新后使用@DataJpaTest 的引导存储库测试失败 - Spring Boot Repository Test with @DataJpaTest fails after update Spring Boot DataJpaTest(用于存储库)因 java.lang.IllegalStateException 失败:无法加载 ApplicationContext - Spring Boot DataJpaTest (for Repositories) fail with java.lang.IllegalStateException: Failed to load ApplicationContext 没有DDL生成的Spring Boot @DataJpaTest - Spring Boot @DataJpaTest without ddl generation DataJpaTest注解用法[Spring-boot] - DataJpaTest annotation usage[Spring-boot] 带有Flyway 4.2.0的Spring Boot 2.1.0 - Spring Boot 2.1.0 with Flyway 4.2.0 无法执行目标 org.springframework.boot:spring-boot-maven-plugin:2.1.0.RELEASE - Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.0.RELEASE 如何使用带有SpringFox @ EnableSwagger2的@DataJpaTest在Spring Boot 1.4中进行切片测试 - How do do slice testing in Spring Boot 1.4 using @DataJpaTest with SpringFox @EnableSwagger2 使用Oracle数据库配置的Spring Boot @DataJpaTest无法获取数据 - Spring boot @DataJpaTest configured with Oracle database not fetching data Spring Boot 1.4 @DataJpaTest - 创建名为“dataSource”的 bean 时出错 - Spring Boot 1.4 @DataJpaTest - Error creating bean with name 'dataSource' Spring Boot 2.1.0和Flyway 4.2.0-测试 - Spring Boot 2.1.0 and Flyway 4.2.0 - Tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM