Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions pom.xml
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>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unreadable indentation

<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>
13 changes: 13 additions & 0 deletions src/main/java/com/nature/pmock/App.java
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!" );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have the log dependency defined. use it.

}
}
29 changes: 29 additions & 0 deletions src/main/java/com/nature/pmock/Controller.java
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;
}
}
}

19 changes: 19 additions & 0 deletions src/main/java/com/nature/pmock/Then.java
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;
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/nature/pmock/AppTest.java
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");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log....

}
}
56 changes: 56 additions & 0 deletions src/test/java/com/nature/pmock/MockTest.java
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");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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();
}
}
}