-
Notifications
You must be signed in to change notification settings - Fork 0
started and completed pmock example #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.nature.pmock</groupId> | ||
| <artifactId>PowerMock</artifactId> | ||
| <packaging>war</packaging> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <name>PowerMock</name> | ||
| <url>http://maven.apache.org</url> | ||
| <repositories> | ||
| <repository> | ||
| <snapshots> | ||
| <enabled>false</enabled> | ||
| </snapshots> | ||
| <id>central</id> | ||
| <name>Maven Repository Switchboard</name> | ||
| <url>http://repo1.maven.org/maven2</url> | ||
| </repository> | ||
| </repositories> | ||
| <properties> | ||
| <powermock.version>1.5.1</powermock.version> | ||
| </properties> | ||
| <!--from powermock-examples --> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>log4j</groupId> | ||
| <artifactId>log4j</artifactId> | ||
| <version>1.2.17</version> | ||
| </dependency> | ||
| <dependency> | ||
|
|
||
| <groupId>junit</groupId> | ||
|
|
||
| <artifactId>junit</artifactId> | ||
|
|
||
| <version>4.9</version> | ||
|
|
||
| <scope>test</scope> | ||
|
|
||
| </dependency> | ||
|
|
||
| <dependency> | ||
|
|
||
| <groupId>org.powermock</groupId> | ||
|
|
||
| <artifactId>powermock-core</artifactId> | ||
|
|
||
| <version>${powermock.version}</version> | ||
|
|
||
| <scope>test</scope> | ||
|
|
||
| </dependency> | ||
|
|
||
| <dependency> | ||
|
|
||
| <groupId>org.easymock</groupId> | ||
|
|
||
| <artifactId>easymock</artifactId> | ||
|
|
||
| <version>3.0</version> | ||
|
|
||
| <scope>test</scope> | ||
|
|
||
| </dependency> | ||
|
|
||
| <dependency> | ||
|
|
||
| <groupId>org.powermock</groupId> | ||
|
|
||
| <artifactId>powermock-module-junit4</artifactId> | ||
|
|
||
| <version>${powermock.version}</version> | ||
|
|
||
| <scope>test</scope> | ||
|
|
||
| </dependency> | ||
|
|
||
| <dependency> | ||
|
|
||
| <groupId>org.powermock</groupId> | ||
|
|
||
| <artifactId>powermock-api-easymock</artifactId> | ||
|
|
||
| <version>${powermock.version}</version> | ||
|
|
||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <!-- Compiler --> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>2.0.2</version> | ||
| <configuration> | ||
| <source>1.6</source> | ||
| <target>1.6</target> | ||
| <encoding>ISO-8859-1</encoding> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>2.13</version> | ||
| <configuration> | ||
| <argLine>-Xms2048m -Xmx2048m -XX:PermSize=2048m | ||
| -XX:MaxPermSize=2048m</argLine> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <reporting> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-report-plugin</artifactId> | ||
| <version>2.14</version> | ||
| </plugin> | ||
| </plugins> | ||
| </reporting> | ||
| </project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.nature.pmock; | ||
|
|
||
| /** | ||
| * Hello world! | ||
| * | ||
| */ | ||
| public class App | ||
| { | ||
| public static void supfools() | ||
| { | ||
| System.out.println( "Hello World!" ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have the log dependency defined. use it. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log.... |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log! |
||
| 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(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unreadable indentation