0

我想测试我是否可以将正确的数据插入数据库。为此,我使用 mock 作为我的服务层。我知道我编写测试工作的方法是正确的。对于选择数据的所有方法,我都会得到正确的结果。但是当我尝试插入数据并返回最后插入的记录 id 时,我得到 0。

public class SeasonServiceImplTest extends BaseServiceTest {

    private static final String SEASON_NAME = "2012-2013";

    private SeasonServiceImpl seasonService;
    private SeasonDao seasonDao;
    private LanguageDao languageDao;

    public SeasonServiceImplTest() {
    }

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();

        seasonService = createService(SeasonServiceImpl.class);

        seasonDao = mock(SeasonDao.class);
        when(getDap().getDao(SeasonDao.class)).thenReturn(seasonDao);

        languageDao = mock(LanguageDao.class);
        when(getDap().getDao(LanguageDao.class)).thenReturn(languageDao);
    }

    private SeasonBean createSeasonBean(){
        SeasonBean seasonBean = new SeasonBean();
        Long langId = 1L;
        seasonBean.setId(langId);
        seasonBean.setSeason(SEASON_NAME);
        Map<Long, String> seasonNames = new HashMap<>();
        seasonNames.put(langId, SEASON_NAME);
        seasonBean.setSeasons(seasonNames);
        seasonBean.setBeach(true);
        return seasonBean;
    }

    @Test
    public void testAddSeason() throws TechnicalException, LogicalException {
        Long idAddedSeason = seasonService.addSeason(createSeasonBean());
        SeasonBean selectedSeasonBean = seasonService.getSeasonById(idAddedSeason);
        assertNotNull("Season wasn't added", selectedSeasonBean);
    }
}

并插入 DAO

//addSeason in DAO layer
@Override
    public Long insert(T entity) {
        getSession().save(entity);
        return (Long) getSession().getIdentifier(entity);
    }

为什么最后插入的记录id可以等于0?

4

0 回答 0