junit

junit常用注解

你说的曾经没有我的故事 提交于 2020-01-18 18:21:02
在junit中常用的注解有@Test、@Ignore、@BeforeClass、@AfterClass、@Before、@After、@Runwith、@Parameters JUnit4的测试类不用再继承TestCase类了。使用注解会方便很多。 @Test 测试方法,在这里可以测试期望异常和超时时间 这是两种抛异常的方法 举例如下:expected方法期望抛出异常 timeout方法举例:期望在100秒内结束测试,如果100秒内没有结束,则认为测试失败(一般测试执行效率可用timeout方法) @Ignore:忽略的测试方法 举例加入@Ignore此方法,在测试时下面这个方法不进行测试(加这个方法是有意义的,比如此方法的功能没有编写完成加入 @Ignore可暂时不对其进行测试) @Before:每个测试方法之前执行(初始化方法),@After:每个测试方法之后执行(释放资源) @BeforeClass:针对所有测试,只执行一次,且必须为static void,在所有的方法之前执行比@Before还要前 @AfterClass:针对所有测试,只执行一次,且必须为static void,在所有的方法之后执行比@After还要后 在什么样的情况下是用这两种方法:如果当你做测试的时候,需要取得很耗费时间的资源比如数据库,或者搭建复杂环境的时候用@BeforeClass

Feasible to start writing unit test in an old project?

自古美人都是妖i 提交于 2020-01-17 12:13:24
问题 I would like to start writing unit test (JUnit) for our project. This uses J2EE 1.4 with Hibernate 3.1 and there is a tight coupling between connection code and service layer(servlets if I am right? correct me!). So suppose I have functionality to persist some form values. The structure is something like, MyServlet.java public void doGet(ServletRequest request, ServletResponse response) { T_Donation instance - new T_Donation(); instance.setName(request.getParameter("name")); instance

JUnit Mockito framework

对着背影说爱祢 提交于 2020-01-17 06:05:54
问题 I am learning the JUnit with Mockito framework, I tried writing test cases on my service code as:- ChildClass childClass = (ChildClass)(employeeDao.callMethod().getClassRef()); JUnit test case:- ChildClass childClass = new ChildClass(); Mockito.when(employeeDao.callMethod().getClassRef()).thenReturn(childClass); But getting java.lang.NullPointerException Then tried splitting the method calls in two seperate statements like:- ChildClass childClass = new ChildClass(); Mockito.when(employeeDao

JUnit Mockito framework

倖福魔咒の 提交于 2020-01-17 06:05:35
问题 I am learning the JUnit with Mockito framework, I tried writing test cases on my service code as:- ChildClass childClass = (ChildClass)(employeeDao.callMethod().getClassRef()); JUnit test case:- ChildClass childClass = new ChildClass(); Mockito.when(employeeDao.callMethod().getClassRef()).thenReturn(childClass); But getting java.lang.NullPointerException Then tried splitting the method calls in two seperate statements like:- ChildClass childClass = new ChildClass(); Mockito.when(employeeDao

Failed to mock ArrayList with option, CALLS_REAL_METHODS

心已入冬 提交于 2020-01-17 01:22:30
问题 According to the official doc: http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#CALLS_REAL_METHODS I wrote the following test: package tk.puncha.study; import org.junit.Test; import java.util.ArrayList; import static org.mockito.Mockito.*; public class MockitoTest { @Test public void test() { ArrayList arrayList = mock(ArrayList.class, withSettings().defaultAnswer((CALLS_REAL_METHODS))); arrayList.add(new Object()); } } But it crashes with the following exception: java

Mocking statement when the input parameter of method as exception

时光毁灭记忆、已成空白 提交于 2020-01-16 20:06:12
问题 I'm writing Junit test case for the below scenario and need some suggestions to cover the below snippet: ErrorHandlingUtils.manageException(new InvalidInputException("Ethernet Ring Name not found"),methodName); I tried passing ringName as null or empty but not sure how to mock exception block. Can someone please give a suggestion? public void deleteEthernetRing(String ringName, String userID) throws Exception { LOGGER.info("Delete Ethernet Ring "); String methodName = "Delete Ethernet Ring ";

UnfinishedStubbingException when working with doNothing method for void methods

限于喜欢 提交于 2020-01-16 19:05:38
问题 The below code is causing UnfinishedStubbingException PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString()); verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update1"), Matchers.eq(jsonObject), anyString()); org.mockito.exceptions.misusing

UnfinishedStubbingException when working with doNothing method for void methods

荒凉一梦 提交于 2020-01-16 19:04:09
问题 The below code is causing UnfinishedStubbingException PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString()); verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update1"), Matchers.eq(jsonObject), anyString()); org.mockito.exceptions.misusing

Testing ServiceLocator using JUnit

不问归期 提交于 2020-01-16 16:59:04
问题 This is a follow up question to my previous question. I am trying to write test case for my ServiceLocator class but it gives me the following error: com/iplanet/ias/admin/common/ASException java.lang.NoClassDefFoundError: com/iplanet/ias/admin/common/ASException at java.lang.ClassLoader.defineClass1(Native Method) My test case: public void testServiceLocator () throws UivException, NamingException { DataSource ds = ServiceLocator.getInstance().getDataSource("jdbc/RSRC/my/mydb"); //I have not

Testing ServiceLocator using JUnit

隐身守侯 提交于 2020-01-16 16:58:08
问题 This is a follow up question to my previous question. I am trying to write test case for my ServiceLocator class but it gives me the following error: com/iplanet/ias/admin/common/ASException java.lang.NoClassDefFoundError: com/iplanet/ias/admin/common/ASException at java.lang.ClassLoader.defineClass1(Native Method) My test case: public void testServiceLocator () throws UivException, NamingException { DataSource ds = ServiceLocator.getInstance().getDataSource("jdbc/RSRC/my/mydb"); //I have not