Age.java と AgeTest.java を以下に示します。
また、 eclipse で emma プラグインをつかってカバレージもとりながらテストを実行したときのスクリーンショットも示します。
参考情報として以下を参照してください。
Age.java
java
1
2 import java . io . BufferedReader ;
3 import java . io . IOException ;
4 import java . io . InputStreamReader ;
5
6 public class Age {
7
8 public static void main ( String [ ] args ) throws IOException {
9 BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ;
10 System . out . println ( "年齢を教えてください" ) ;
11 int x = Integer . parseInt ( br . readLine ( ) ) ;
12
13 if ( x >= 20 ) {
14 System . out . println ( "成人ですね。" ) ;
15 } else {
16 System . out . println ( "未成年ですね。" ) ;
17 }
18 }
19 }
AgeTest.java
java
1
2 import static org . junit . Assert . assertEquals ;
3
4 import java . io . ByteArrayOutputStream ;
5 import java . io . IOException ;
6 import java . io . PrintStream ;
7
8 import org . junit . After ;
9 import org . junit . Before ;
10 import org . junit . Test ;
11
12 public class AgeTest {
13 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream ( ) ;
14 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream ( ) ;
15 private final StandardInputSnatcher in = new StandardInputSnatcher ( ) ;
16
17 @Before
18 public void setUpStreams ( ) {
19 System . setOut ( new PrintStream ( outContent ) ) ;
20 System . setErr ( new PrintStream ( errContent ) ) ;
21 System . setIn ( in ) ;
22 }
23
24 @After
25 public void cleanUpStreams ( ) {
26 System . setOut ( System . out ) ;
27 System . setErr ( System . err ) ;
28 System . setIn ( System . in ) ;
29 }
30
31 @Test
32 public void testAdult ( ) throws IOException {
33 String [ ] args = { } ;
34 in . inputln ( "21" ) ;
35 Age . main ( args ) ;
36 assertEquals ( "21歳は成人" , "年齢を教えてください\n成人ですね。\n" , outContent . toString ( ) ) ;
37 }
38
39 @Test
40 public void testChild ( ) throws IOException {
41 String [ ] args = { } ;
42 in . inputln ( "19" ) ;
43 Age . main ( args ) ;
44 // System.out.print("hello");
45 assertEquals ( "19歳は未成年" , "年齢を教えてください\n未成年ですね。\n" , outContent . toString ( ) ) ;
46 }
47
48 @Test
49 public void test20 ( ) throws IOException {
50 String [ ] args = { } ;
51 in . inputln ( "20" ) ;
52 Age . main ( args ) ;
53 // System.out.print("hello");
54 assertEquals ( "20歳は成人" , "年齢を教えてください\n成人ですね。\n" , outContent . toString ( ) ) ;
55 }
56 }
StandardInputSnatcher.java
java
1 import java . io . IOException ;
2 import java . io . InputStream ;
3
4 // See http://qiita.com/Takmiy/items/ea234d2211aba66e7659
5 // > JUnitで標準入力をつっこんで流す
6
7 9
10 public class StandardInputSnatcher extends InputStream {
11
12 private StringBuilder buffer = new StringBuilder ( ) ;
13 private static String crlf = System . getProperty ( "line.separator" ) ;
14
15 18 19
20 public void inputln ( String str ) {
21 buffer . append ( str ) . append ( crlf ) ;
22 }
23
24 @Override
25 public int read ( ) throws IOException {
26 if ( buffer . length ( ) == 0 ) {
27 return - 1 ;
28 }
29 int result = buffer . charAt ( 0 ) ;
30 buffer . deleteCharAt ( 0 ) ;
31 return result ;
32 }
33 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。