数据库表
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;
并插入主板 mybatis映射器
<insert id="insertBoard" parameterType="hashmap">
<![CDATA[
INSERT INTO board
(
password
)
VALUES
(
#{password}
)
]]>
</insert>
Junit测试类
@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);
}
}
问题是我在Junit Test Class 'BoardServiceTest'
中插入insertBoard总是b_index设置'0'
所以当我在 BoardServiceTest类
中尝试这样的时候@Test
@Transactional
public void validPassword() {
String pwd = boardService.selectPassword(board);
assertEquals(TEST_PASSWORD, pwd);
}
此测试始终为FALSE,因为板对象索引为'0'
但实际上b_index
不是'0',因为AUTO_INCREMENT
不是'0'
@Test
@Transactional
public void getAllBoard() {
List<Board> selectAllBoardList = boardService.selectAllBoardList();
System.out.println("TEST : " + selectAllBoardList.get(0).getB_index());
assertEquals(false, selectAllBoardList.isEmpty());
}
此测试日志TEST : 10
如何在这种情况下在JUnit中测试数据库?
当我尝试JUnit测试时,总是需要设置AUTO_INCREMENT = 0吗?
答案 0 :(得分:0)
@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");
boardService.insertBoard(board);
this.board = board;
}
}
在insertBoard mybatis之前
键是自动设置AUTO_INCREMENT PK