Best Jmock-library code snippet using org.jmock.test.acceptance.MockeryFinalizationAcceptanceTests.install
Source:MockeryFinalizationAcceptanceTests.java
...26 private final ErrorStream capturingErr = new ErrorStream();27 @BeforeAll28 public static void clearAnyOutstandingMessages() {29 ErrorStream localErr = new ErrorStream();30 localErr.install();31 String error = null;32 try {33 finalizeUntilMessageOrCount(localErr, FINALIZE_COUNT);34 error = localErr.output();35 } finally {36 localErr.uninstall();37 }38 if (error != null)39 System.err.println("WARNING - a previous test left output in finalization [" + error + "]");40 }41 @BeforeEach42 public void captureSysErr() {43 capturingErr.install();44 }45 @AfterEach46 public void replaceSysErr() {47 capturingErr.uninstall();48 }49 @ParameterizedTest50 @ArgumentsSource(ImposteriserParameterResolver.class)51 public void mockedInterfaceDoesntWarnOnFinalize(Imposteriser imposterImpl) {52 mockery.setImposteriser(imposterImpl);53 54 checkNoFinalizationMessage(mockery, CharSequence.class);55 }56 @ParameterizedTest57 @ArgumentsSource(CodeGeneratingImposteriserParameterResolver.class)58 public void mockedClassDoesntWarnOnFinalize(Imposteriser imposterImpl) {59 mockery.setImposteriser(imposterImpl);60 checkNoFinalizationMessage(mockery, Object.class);61 }62 public interface TypeThatMakesFinalizePublic {63 public void finalize();64 }65 @ParameterizedTest66 @ArgumentsSource(ImposteriserParameterResolver.class)67 public void mockedTypeThatMakesFinalizePublicDoesntWarnOnFinalize(Imposteriser imposterImpl) {68 mockery.setImposteriser(imposterImpl);69 checkNoFinalizationMessage(mockery, TypeThatMakesFinalizePublic.class);70 }71 private void checkNoFinalizationMessage(Mockery mockery, Class<?> typeToMock) {72 WeakReference<Object> mockHolder = new WeakReference<Object>(mockery.mock(typeToMock));73 while (mockHolder.get() != null) {74 System.gc();75 System.runFinalization();76 }77 finalizeUntilMessageOrCount(capturingErr, FINALIZE_COUNT);78 assertThat(capturingErr.output(), isEmptyOrNullString());79 }80 private static void finalizeUntilMessageOrCount(ErrorStream capturingErr, int count) {81 for (int i = 0; i < count && capturingErr.output().isEmpty(); i++) {82 System.gc();83 System.runFinalization();84 }85 }86 private static class ErrorStream extends PrintStream {87 private PrintStream oldSysErr;88 public ErrorStream() {89 super(new ByteArrayOutputStream());90 }91 public void install() {92 oldSysErr = System.err;93 System.setErr(this);94 }95 public void uninstall() {96 System.setErr(oldSysErr);97 }98 public String output() {99 return new String(((ByteArrayOutputStream) out).toByteArray());100 }101 }102}...
install
Using AI Code Generation
1 private static final String[] MOCKERY_FINALIZATION_ACCEPTANCE_TESTS = new String[] {2 "package org.jmock.test.acceptance;",3 "import org.jmock.Expectations;",4 "import org.jmock.Mockery;",5 "import org.jmock.api.ExpectationError;",6 "import org.jmock.lib.legacy.ClassImposteriser;",7 "import org.junit.Test;",8 "public class MockeryFinalizationAcceptanceTests {",9 " @Test(expected=ExpectationError.class)",10 " public void canDetectUnmetExpectationsWhenFinalized() {",11 " Mockery context = new Mockery();",12 " context.setImposteriser(ClassImposteriser.INSTANCE);",13 " final Runnable mockRunnable = context.mock(Runnable.class);",14 " context.checking(new Expectations() {{",15 " oneOf (mockRunnable).run();",16 " }});",17 " context.assertIsSatisfied();",18 " context = null;",19 " System.gc();",20 " }",21 "}"22 };23 private static final String[] MOCKERY_FINALIZATION_ACCEPTANCE_TESTS_EXPECTED = new String[] {24 "package org.jmock.test.acceptance;",25 "import org.jmock.Expectations;",26 "import org.jmock.Mockery;",27 "import org.jmock.api.ExpectationError;",28 "import org.jmock.lib.legacy.ClassImposteriser;",29 "import org.junit.Test;",30 "public class MockeryFinalizationAcceptanceTests {",31 " @Test(expected=ExpectationError.class)",32 " public void canDetectUnmetExpectationsWhenFinalized() {",33 " Mockery context = new Mockery();",34 " context.setImposteriser(ClassImposteriser.INSTANCE);",35 " final Runnable mockRunnable = context.mock(Runnable.class);",36 " context.checking(new Expectations() {{",37 " oneOf (mockRunnable).run();",38 " }});",39 " context.assertIsSatisfied();",40 " context = null;",41 " System.gc();",42 " }",43 "}"44 };
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!!