[问题] 关于mybatis的uni test

楼主: angleevil (朦胧寻光)   2018-02-12 14:57:40
由于公司要求mybatis产生的code gen也需要uni-test.
参考此网站
https://github.com/mybatis/mybatis-3/wiki/Unit-Test
撰写以下测试程式, 我先用befor去add资料, 再用test去测试select的资料,但是每次得到都是null. 目前理解是会存到in-memory database.
所以不会对真实的db做操作, 但是不理解为何无法select资料.不知道大家有什么方向吗??
public class BaseTest {
protected static SqlSessionFactory sqlSessionFactory;
@Inject
private CommunityDAO communityDAO;
private final Integer page = 1;
private final Integer range = 8;
private Integer communityId = 0;
private final String name = "comm1";
private final String desc = "desc1";
private final String userId = "u000001";
private final String nameUpdate = "comm2";
private final String descUpdate = "desc2";
private final String userIdUpdate = "u000002";
@BeforeClass
public static void setUpClass() throws Exception {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
SqlSession session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
reader = Resources.getResourceAsReader("sql/schema.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.runScript(reader);
conn.close();
reader.close();
session.close();
}
@Before
public void setUp() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
communityDAO = sqlSession.getMapper(CommunityDAO.class);
CommunityInfo communityInfo = new CommunityInfo();
Date dNow = new Date();
long milliseconds = dNow.getTime();
communityInfo.setName(name);
communityInfo.setDesc(desc);
communityInfo.setUserId(userId);
communityInfo.setDate(milliseconds);
communityInfo.setType(TypeEnum.PUBLIC.toString());
communityInfo.setStatus(StatusEnum.OPEN.toString());
communityInfo.setCategory(CategoryEnum.COMMON.toString());
communityDAO.add(communityInfo);<==add 资料
communityId = communityInfo.getCommunityId(); <== 此sql的字段是community_id是auto_increment会自动增长
} finally {
sqlSession.close();
}
}
@Test
public void getCommunityById() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
communityDAO = sqlSession.getMapper(CommunityDAO.class);
CommunityInfo communityInfo = communityDAO.getCommunityById(communityId); <== select资料
System.out.println(communityInfo+"xxxxxxxxxxxxxx"); <==== 每次都是null
Assert.assertEquals("" , communityInfo.getName()); <====所以都不过
} finally {
sqlSession.close();
}
}
}
作者: kniver999 (小囧)   2018-02-12 15:31:00
塞完后在同一个方法中马上查询有东西吗?
楼主: angleevil (朦胧寻光)   2018-02-12 15:47:00
刚刚主管帮忙看,add完后, 在select就有. 但是分两段第二段api的select就没东西, 可是看范例, 不是存在内存嘛??

Links booklink

Contact Us: admin [ a t ] ucptt.com