Best Easymock code snippet using org.easymock.tests2.EasyMockPropertiesTest.resetInstance
Source:EasyMockPropertiesTest.java
...60 public static void afterClass() throws Exception {61 // Cleanup the mess62 PROPERTIES_FILE.delete();63 // Make sure we reset all our mess at the end64 resetInstance();65 }66 @Before67 public void setup() throws Exception {68 // Make sure to reset to prevent getting an already initialized69 // EasyMockProperties70 resetInstance();71 // Set manually a new one72 setEasyMockProperty("easymock.e", "7");73 // Set manually an old one74 setEasyMockProperty("easymock.c", "8");75 // Overload after (will be ignored)76 System.setProperty("easymock.h", "4");77 }78 @Test79 public void testGetInstance() {80 assertExpectedValue("1", "easymock.a");81 assertExpectedValue("8", "easymock.c");82 assertExpectedValue("7", "easymock.e");83 assertExpectedValue(null, "easymock.g");84 assertExpectedValue(null, "easymock.h");85 assertExpectedValue(null, "xxx.yyy");86 assertExpectedValue(Boolean.TRUE.toString(), EasyMock.NOT_THREAD_SAFE_BY_DEFAULT);87 assertExpectedValue(null, EasyMock.DISABLE_CLASS_MOCKING);88 }89 @Test90 public void testGetProperty() {91 EasyMockProperties instance = EasyMockProperties.getInstance();92 // use the default93 assertEquals("1", instance.getProperty("easymock.a", "10"));94 // don't use the default95 assertEquals("10", instance.getProperty("easymock.z", "10"));96 // null default97 assertNull(instance.getProperty("easymock.z", null));98 }99 @Test(expected = IllegalArgumentException.class)100 public void testSetProperty() {101 EasyMockProperties instance = EasyMockProperties.getInstance();102 instance.setProperty("tralala.a", null);103 }104 @Test105 public void testNoThreadContextClassLoader() throws Exception {106 ClassLoader old = Thread.currentThread().getContextClassLoader();107 try {108 resetInstance();109 // Remove the context class loader110 Thread.currentThread().setContextClassLoader(null);111 // This instance will load easymock.properties from the112 // EasyMockProperties class loader113 EasyMockProperties.getInstance();114 // And so "easymock.a" should be there115 assertExpectedValue("1", "easymock.a");116 } finally {117 // Whatever happens, set the initial class loader back or it'll get118 // messy119 Thread.currentThread().setContextClassLoader(old);120 }121 }122 @Test123 public void testBadPropertiesFile() throws Exception {124 final boolean[] close = new boolean[1];125 // A ClassLoader that returns no easymock.properties126 ClassLoader cl = new ClassLoader(getClass().getClassLoader()) {127 @Override128 public InputStream getResourceAsStream(String name) {129 if ("easymock.properties".equals(name)) {130 return new InputStream() {131 @Override132 public void close() {133 close[0] = true;134 }135 @Override136 public int read(byte[] b, int off, int len) throws IOException {137 throw new IOException("Failed!");138 }139 @Override140 public int read(byte[] b) throws IOException {141 throw new IOException("Failed!");142 }143 @Override144 public int read() throws IOException {145 throw new IOException("Failed!");146 }147 };148 }149 return super.getResourceAsStream(name);150 }151 };152 ClassLoader old = Thread.currentThread().getContextClassLoader();153 try {154 resetInstance();155 // Remove the context class loader156 Thread.currentThread().setContextClassLoader(cl);157 try {158 EasyMockProperties.getInstance();159 fail("Should have an issue loading the easymock.properties file");160 } catch (RuntimeException e) {161 assertEquals("Failed to read easymock.properties file", e.getMessage());162 // Make sure the thread was closed163 assertTrue(close[0]);164 }165 } finally {166 // Whatever happens, set the initial class loader back or it'll get167 // messy168 Thread.currentThread().setContextClassLoader(old);169 }170 }171 @Test172 public void testNoEasymockPropertiesFile() throws Exception {173 // A ClassLoader that returns no easymock.properties174 ClassLoader cl = new ClassLoader(getClass().getClassLoader()) {175 @Override176 public InputStream getResourceAsStream(String name) {177 if ("easymock.properties".equals(name)) {178 return null;179 }180 return super.getResourceAsStream(name);181 }182 };183 ClassLoader old = Thread.currentThread().getContextClassLoader();184 try {185 resetInstance();186 // Set our class loader187 Thread.currentThread().setContextClassLoader(cl);188 // This instance will try to load easymock.properties with our189 // custom class loader and so won't find it190 EasyMockProperties.getInstance();191 // And so it shouldn't find "easymock.a"192 assertExpectedValue(null, "easymock.a");193 } finally {194 // Whatever happens, set the initial class loader back or it'll get195 // messy196 Thread.currentThread().setContextClassLoader(old);197 }198 }199 private static void resetInstance() throws NoSuchFieldException, IllegalAccessException {200 // Cheat and make the singleton uninitialized201 Field field = EasyMockProperties.class.getDeclaredField("instance");202 field.setAccessible(true);203 field.set(null, null);204 }205 private static void assertExpectedValue(String expected, String key) {206 assertEquals(expected, getEasyMockProperty(key));207 }208}...
resetInstance
Using AI Code Generation
1org.easymock.tests2.EasyMockPropertiesTest.resetInstance();2org.easymock.tests2.EasyMockPropertiesTest.resetInstance();3org.easymock.tests2.EasyMockPropertiesTest.resetInstance();4org.easymock.tests2.EasyMockPropertiesTest.resetInstance();5org.easymock.tests2.EasyMockPropertiesTest.resetInstance();6org.easymock.tests2.EasyMockPropertiesTest.resetInstance();7org.easymock.tests2.EasyMockPropertiesTest.resetInstance();8org.easymock.tests2.EasyMockPropertiesTest.resetInstance();9org.easymock.tests2.EasyMockPropertiesTest.resetInstance();10org.easymock.tests2.EasyMockPropertiesTest.resetInstance();11org.easymock.tests2.EasyMockPropertiesTest.resetInstance();12org.easymock.tests2.EasyMockPropertiesTest.resetInstance();13org.easymock.tests2.EasyMockPropertiesTest.resetInstance();14org.easymock.tests2.EasyMockPropertiesTest.resetInstance();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!