package demo.org.powermock.examples.simple;

import static org.powermock.PowerMock.createMock;
import static org.powermock.PowerMock.expectNew;
import static org.powermock.PowerMock.replayAll;
import static org.powermock.PowerMock.verifyAll;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith(PowerMockRunner.class)
@PrepareForTest({Logger.class})
public class LoggerTest {
	
	@Test(expected=IllegalStateException.class)
	public void testException() throws Exception {
		expectNew(FileWriter.class, "logger.log").andThrow(new IOException());
		
		replayAll();
		new Logger();
	}
	
	@Test
	public void testLogger() throws Exception {
		PrintWriter printWriter = createMock(PrintWriter.class);
		printWriter.println("qwe");
		expectNew(PrintWriter.class, new Class[] { Writer.class }, new Object[] { EasyMock.anyObject() }).andReturn(printWriter);
		replayAll();
		Logger logger = new Logger();
		logger.log("qwe");
		verifyAll();
	}

}

@@ -186,16 +186,75 @@
 layAll;%0A
+import static org.powermock.PowerMock.suppressConstructor;%0A
 import s
@@ -488,16 +488,47 @@
 unWith;%0A
+import org.powermock.Whitebox;%0A
 import o
@@ -1142,23 +1142,8 @@
 s %7D,
- new Object%5B%5D %7B
  Eas
@@ -1159,18 +1159,16 @@
 Object()
- %7D
 ).andRet
@@ -1272,10 +1272,326 @@
 ();%0A%09%7D%0A%0A
+%0A%09@Test%0A%09public void testLogger2() throws Exception %7B%0A%09%09PrintWriter printWriter = createMock(PrintWriter.class);%0A%09%09printWriter.println(%22qwe%22);%0A%09%09suppressConstructor(Logger.class);%0A%09%09replayAll();%0A%09%09Logger logger = new Logger();%0A%09%09Whitebox.setInternalState(logger, printWriter);%0A%09%09logger.log(%22qwe%22);%0A%09%09verifyAll();%0A%09%7D%0A
 %7D%0A

Update before demo at oredev