diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8c47ce9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,123 @@ + + 4.0.0 + com.nature.pmock + PowerMock + war + 1.0-SNAPSHOT + PowerMock + http://maven.apache.org + + + + false + + central + Maven Repository Switchboard + http://repo1.maven.org/maven2 + + + + 1.5.1 + + + + + log4j + log4j + 1.2.17 + + + + junit + + junit + + 4.9 + + test + + + + + + org.powermock + + powermock-core + + ${powermock.version} + + test + + + + + + org.easymock + + easymock + + 3.0 + + test + + + + + + org.powermock + + powermock-module-junit4 + + ${powermock.version} + + test + + + + + + org.powermock + + powermock-api-easymock + + ${powermock.version} + + + + + + + + + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + ISO-8859-1 + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.13 + + -Xms2048m -Xmx2048m -XX:PermSize=2048m + -XX:MaxPermSize=2048m + + + + + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.14 + + + + diff --git a/src/main/java/com/nature/pmock/App.java b/src/main/java/com/nature/pmock/App.java new file mode 100644 index 0000000..5d5c9e1 --- /dev/null +++ b/src/main/java/com/nature/pmock/App.java @@ -0,0 +1,13 @@ +package com.nature.pmock; + +/** + * Hello world! + * + */ +public class App +{ + public static void supfools() + { + System.out.println( "Hello World!" ); + } +} diff --git a/src/main/java/com/nature/pmock/Controller.java b/src/main/java/com/nature/pmock/Controller.java new file mode 100644 index 0000000..2a4c252 --- /dev/null +++ b/src/main/java/com/nature/pmock/Controller.java @@ -0,0 +1,29 @@ +package com.nature.pmock; + +import java.util.Date; + +public class Controller { + + public Date processThen() { + Date date = Then.getThen(); + if (moreThenAWeek(date, new Date())) { + throw new RuntimeException("too late"); + } + return date; + } + + private boolean moreThenAWeek(Date date, Date currentDate){ + long dateTime = date.getTime(); + long currDateTime = currentDate.getTime(); + long difference = dateTime - currDateTime; + long week = 1000 * 60 * 60 * 24 * 7; + week = -week; + if(difference < week){ + return true; + } + else{ + return false; + } + } +} + diff --git a/src/main/java/com/nature/pmock/Then.java b/src/main/java/com/nature/pmock/Then.java new file mode 100644 index 0000000..317902f --- /dev/null +++ b/src/main/java/com/nature/pmock/Then.java @@ -0,0 +1,19 @@ +package com.nature.pmock; + +import java.util.Date; + +public class Then { + + private static Date date; + + private Then() { + super(); + } + + public static Date getThen() { + if(date == null){ + date = new Date(); + } + return date; + } +} \ No newline at end of file diff --git a/src/test/java/com/nature/pmock/AppTest.java b/src/test/java/com/nature/pmock/AppTest.java new file mode 100644 index 0000000..96f1ad1 --- /dev/null +++ b/src/test/java/com/nature/pmock/AppTest.java @@ -0,0 +1,15 @@ +package com.nature.pmock; + +import org.junit.Test; + + +/** + * Unit test for simple App. + */ +public class AppTest { + + @Test + public void RunTest(){ + System.out.println("run a test"); + } +} diff --git a/src/test/java/com/nature/pmock/MockTest.java b/src/test/java/com/nature/pmock/MockTest.java new file mode 100644 index 0000000..7d4aca2 --- /dev/null +++ b/src/test/java/com/nature/pmock/MockTest.java @@ -0,0 +1,56 @@ +package com.nature.pmock; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import java.util.Date; + +import org.easymock.EasyMock; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest( { Then.class }) +public class MockTest{ + + @Test + public void ValidDate(){ + try { + System.out.println("------------------- ValidDate"); + PowerMock.mockStatic(Then.class); + EasyMock.expect(Then.getThen()).andReturn(new Date()); + Controller c = new Controller(); + PowerMock.replayAll(); + Date date = c.processThen(); + assertNotNull(date); + PowerMock.verifyAll(); + } catch (Exception e){ + System.out.println(e); + fail(); + } + } + + @Test + public void InvalidDate(){ + try{ + System.out.println("------------------- InvalidDate"); + PowerMock.mockStatic(Then.class); + EasyMock.expect(Then.getThen()).andReturn(new Date(1372654800)); + Controller c = new Controller(); + PowerMock.replayAll(); + Date date = c.processThen(); + assertNull(date); + PowerMock.verifyAll(); + } catch(RuntimeException e) { + System.out.println(e); + System.out.println("correct exception recieved"); + } catch(Exception e){ + System.out.println(e); + fail(); + } + } +} \ No newline at end of file