簡體   English   中英

使用DataJpaTest Spring Boot 2.1.0更新失敗

[英]Failed to update using DataJpaTest spring boot 2.1.0

對Spring Boot父版本2.1.0使用單元測試。 將更新應用於Game對象內的List對象后,我需要測試值的更新。 我讀了另一個線程以添加以下語句,但在我的情況下無法使用

  @Modifying(clearAutomatically = true) 

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

@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

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

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

錯誤如下,它是在測試類中第二次調用save的行

 Game afterFirstMove = repository.save(savedGame);

test_save_game(com.game.kalah.repository.GameRepositoryTest)經過的時間:0.106 s <<<錯誤! com.game.kalah.repository.GameRepositoryTest.test_save_game的java.lang.UnsupportedOperationException(GameRepositoryTest.java:85)

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>

您是否嘗試過使用可變列表? Arrays.asList(array)創建一個不可變的列表。

嘗試使用new ArrayList<>(Arrays.asList(array)) 檢查此鏈接

另外,如果您發布整個錯誤日志,也會很有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM