结合使用Dummy, InjectTo, PartialMock的单元测试
结合使用Dummy,InjectTo,PartialMock的单元测试,备忘
@RunWith(UnitilsJUnit4TestClassRunner.class) public class CheckerTest { private PartialMock<Manager> ManagerMock; private PartialMock<Dao1> daoMock; @TestedObject private Checker checker; @TestedObject private MyBean beanMock; @TestedObject private Result result; @TestedObject private MyBean2 bean2; // 在before方法之前执行(自定义) @CreateMock private MyBean bean3; @InjectIntoByType(target = "checker") @Dummy protected Dao1 dao1; @Before public void initMock() { bean3.setId(123L); bean3.setDescPath("abc"); } private void setManager() throws IcException { result.setItem(bean2); ManagerMock.returns(result).queryById(null, null, null); checker.setManager(ManagerMock.getMock()); } @Test public void testCheck() throws Exception { setManager(); beanMock.setId(123L); daoMock.returns(bean3).query(0, 0, null); checker.setDao1(daoMock.getMock()); checker.check(beanMock, "abc"); assertEquals(beanMock.getMemo(), "abc"); } @Test(expected = Exception.class) public void testCheck_和数据库中的id不等() throws Exception { setManager(); beanMock.setId(111L); daoMock.returns(bean3).query(0, 0, null); checker.setDao1(daoMock.getMock()); checker.check(beanMock, "abc"); } @Test public void testCheck_没有指定id() throws Exception { setManager(); daoMock.returns(bean3).query(0, 0, null); checker.setDao1(daoMock.getMock()); checker.check(beanMock, "abc"); assertEquals(beanMock.getMemo(), "abc"); assertEquals(beanMock.getId(), 123L); } @Test(expected = Exception.class) public void testCheck_数据库中不存在() throws Exception { setManager(); daoMock.returns(null).query(0, 0, null); checker.setDao1(daoMock.getMock()); checker.check(beanMock, "abc"); } }
相关推荐
Cocolada 2020-11-12
86427019 2020-06-28
84284855 2020-06-11
snowphy 2020-05-30
蛰脚踝的天蝎 2020-11-10
TuxedoLinux 2020-09-11
snowphy 2020-08-19
83540690 2020-08-16
lustdevil 2020-08-03
83417807 2020-07-19
张文倩数据库学生 2020-07-19
bobljm 2020-07-07
83417807 2020-06-28
86427019 2020-06-25
zhengzf0 2020-06-21
tobecrazy 2020-06-16
宿命java 2020-06-15