jeudi 31 décembre 2015

Spring MVC Junit Test DB auto increament

DB table is

CREATE TABLE `test` (
  `b_index` int(10) NOT NULL AUTO_INCREMENT,
  `password` varchar(40) DEFAULT NULL,
  PRIMARY KEY (`b_index`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

and insert board mybatis mapper is

<insert id="insertBoard" parameterType="hashmap">
    <![CDATA[
        INSERT INTO board
        (
            password
        )
        VALUES
        (
            #{password}
        )
    ]]>
</insert>

Junit Test Class is

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/application-context.xml")
public class BoardServiceTest {
    @Autowired
    private BoardService boardService;

    private static final String TEST_PASSWORD = "test";

    private Board board = null; // test vo object

    @Before
    @Transactional
    @Rollback
    public void setup() throws Exception {
        Board board = null; // test vo object

        board = new HistoryBoard();
        board.setPassword("TEST");

        this.board = board;

        boardService.insertBoard(board);
    }
}

the problem is when i insertBoard in Junit Test Class 'BoardServiceTest'

always b_index set '0'

so when i try like this in BoardServiceTest class

@Test
@Transactional
public void validPassword() {
    String pwd = boardService.selectPassword(board);

    assertEquals(TEST_PASSWORD, pwd);
}

this test always FALSE becuase board object index is '0'

but actually b_index is not '0' because AUTO_INCREMENT is not '0'

@Test
@Transactional
public void getAllBoard() {
    List<Board> selectAllBoardList = boardService.selectAllBoardList();

    System.out.println("TEST : " + selectAllBoardList.get(0).getB_index());

    assertEquals(false, selectAllBoardList.isEmpty());
}

when this this TEST log TEST : 10

how to test DB in JUnit like this case?

Always need to set AUTO_INCREMENT=0 when i try JUnit test?

Aucun commentaire:

Enregistrer un commentaire