Skip to content

Commit 377d8b9

Browse files
committed
Add PowerMockito tests to verify System.exit() behaviour and mock objects.
1 parent 5cb2057 commit 377d8b9

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@
2323
<version>4.11</version>
2424
<scope>test</scope>
2525
</dependency>
26+
<!--<dependency>-->
27+
<!--<groupId>org.mockito</groupId>-->
28+
<!--<artifactId>mockito-core</artifactId>-->
29+
<!--<version>1.9.5</version>-->
30+
<!--<scope>test</scope>-->
31+
<!--</dependency>-->
32+
<dependency>
33+
<groupId>org.powermock</groupId>
34+
<artifactId>powermock-module-junit4</artifactId>
35+
<version>1.5.1</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.powermock</groupId>
40+
<artifactId>powermock-api-mockito</artifactId>
41+
<version>1.5.1</version>
42+
<scope>test</scope>
43+
</dependency>
2644
<dependency>
2745
<groupId>com.github.stefanbirkner</groupId>
2846
<artifactId>system-rules</artifactId>

src/main/java/com/example/hellowithtests/Hello.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
public class Hello {
99

1010
static final String HELLO = "Hello!";
11-
private short times;
11+
public static final int MAXIMUM_AMOUNT_OF_TIMES = 20;
12+
private short times = 1;
1213

1314
/**
14-
* Construct a new Hello instance.
15+
* Set how many times "Hello!" should be said.
1516
*
1617
* @param times How many times should this class say "Hello!"? The value should be no larger than 20.
1718
* @throws IllegalArgumentException Thrown when times is larger than 20 or a negative number.
1819
*/
19-
public Hello(int times) {
20-
if (times < 0 || times > 20) {
21-
throw new IllegalArgumentException("Parameter «times» should be a positive integer no larger than 20.");
20+
public void setTimes(int times) {
21+
if (times < 0 || times > MAXIMUM_AMOUNT_OF_TIMES) {
22+
throw new IllegalArgumentException("Parameter «times» should be a positive integer no larger than "
23+
+ MAXIMUM_AMOUNT_OF_TIMES + ".");
2224
}
2325
this.times = (short) times;
2426
}

src/main/java/com/example/hellowithtests/HelloApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static void main(String[] args) {
2929
}
3030
}
3131

32-
Hello hi = null;
32+
Hello hi = new Hello();
3333
try {
34-
hi = new Hello(times);
34+
hi.setTimes(times);
3535
} catch (IllegalArgumentException e) {
3636
System.err.println("Something went wrong: " + e.getMessage());
3737
System.exit(EXIT_STATUS_HELLO_FAILED);

src/test/java/com/example/hellowithtests/HelloAppTest.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
11
package com.example.hellowithtests;
22

3-
import org.junit.Rule;
43
import org.junit.Test;
5-
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
4+
import org.junit.runner.RunWith;
5+
import org.powermock.api.mockito.PowerMockito;
6+
import org.powermock.core.classloader.annotations.PrepareForTest;
7+
import org.powermock.modules.junit4.PowerMockRunner;
8+
9+
import static org.mockito.Mockito.*;
10+
import static org.powermock.api.mockito.PowerMockito.whenNew;
611

712
/**
813
* Unit test for HelloApp.
914
* <p/>
1015
* A unit test aims to test all code and code paths of a specific class.
16+
* <p/>
17+
* This test uses PowerMock and Mockito to mock objects.
1118
*/
19+
@RunWith(PowerMockRunner.class)
20+
@PrepareForTest({System.class, HelloApp.class})
1221
public class HelloAppTest {
1322

14-
@Rule
15-
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
16-
1723
@Test
1824
public void testMain() {
1925
String[] args = {"1"};
2026
HelloApp.main(args);
2127
}
2228

2329
@Test
24-
public void testHelloError() {
25-
exit.expectSystemExitWithStatus(HelloApp.EXIT_STATUS_HELLO_FAILED);
26-
String[] args = {"21"};
30+
public void testWrongArgument() {
31+
PowerMockito.mockStatic(System.class);
32+
33+
String[] args = {"bicycle"};
2734
HelloApp.main(args);
35+
36+
// Did the program exit with the expected error code?
37+
PowerMockito.verifyStatic(only());
38+
System.exit(HelloApp.EXIT_STATUS_PARAMETER_NOT_UNDERSTOOD);
2839
}
2940

3041
@Test
31-
public void testWrongArgument() {
32-
exit.expectSystemExitWithStatus(HelloApp.EXIT_STATUS_PARAMETER_NOT_UNDERSTOOD);
33-
String[] args = {"bicycle"};
42+
public void testHelloError() throws Exception {
43+
PowerMockito.mockStatic(System.class);
44+
45+
// Mock Hello used by HelloApp to throw the expected exception when invoked with setTimes(5).
46+
Hello hi = mock(Hello.class);
47+
doThrow(new IllegalArgumentException("Nope.")).when(hi).setTimes(5);
48+
// Sneakily insert our fake Hello class when it is created.
49+
whenNew(Hello.class).withNoArguments().thenReturn(hi);
50+
51+
// We know this will raise the expected exception, because we mocked Hello.
52+
String[] args = {"5"};
3453
HelloApp.main(args);
54+
55+
// Did the program exit with the expected error code?
56+
PowerMockito.verifyStatic(only());
57+
System.exit(HelloApp.EXIT_STATUS_HELLO_FAILED);
3558
}
3659

3760
@Test

src/test/java/com/example/hellowithtests/HelloTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void testSayHello() {
2121
OutputStream os = new ByteArrayOutputStream();
2222
PrintStream stream = new PrintStream(os, true);
2323

24-
Hello hi = new Hello(1);
24+
Hello hi = new Hello();
2525
hi.sayHello(stream);
2626

2727
assertThat(os.toString(), is(equalTo(Hello.HELLO + "\n")));
@@ -32,7 +32,8 @@ public void testSayHelloAFewTimes() {
3232
OutputStream os = new ByteArrayOutputStream();
3333
PrintStream stream = new PrintStream(os, true);
3434

35-
Hello hi = new Hello(3);
35+
Hello hi = new Hello();
36+
hi.setTimes(3);
3637
hi.sayHello(stream);
3738

3839
// Does it say "Hello!" three times?
@@ -42,11 +43,13 @@ public void testSayHelloAFewTimes() {
4243

4344
@Test(expected = IllegalArgumentException.class)
4445
public void testIllegalArgumentForHello21() {
45-
new Hello(21);
46+
Hello hi = new Hello();
47+
hi.setTimes(Hello.MAXIMUM_AMOUNT_OF_TIMES + 1);
4648
}
4749

4850
@Test(expected = IllegalArgumentException.class)
4951
public void testIllegalArgumentForHelloNegative() {
50-
new Hello(-1);
52+
Hello hi = new Hello();
53+
hi.setTimes(-1);
5154
}
5255
}

0 commit comments

Comments
 (0)