2016-12-21 8 views
-2

私はMockitoテストに問題があります。 私はモキトを理解していないと認めます。 私は多くのページを読んでいます。私は多くの例を読んでいますが、まだ何も読んでいません。Mockitoメソッドとテストの場合

私はMavenにプログラムを持っています。ファイル名を定義します。ファイルの内容を表示します。 プログラムは、App(条件と表示)、methodApp(メソッド)です。

のApp

public static void main(String[] args) throws IOException { 
    new App(); 
} 
private App() throws IOException { 
    methodApp ViewProgram = new methodApp(); 
    if (ViewProgram.file == null) { 
     out.println("No File!"); 
     return; 
    } 
    out.println(ViewProgram.removeSpacesDisplaysContents()); 
} 

methodAppは

InputStream file = getClass().getResourceAsStream("/"+enterNameFileConsole()); 

private String enterNameFileConsole(){ 
    out.println("Enter filename:"); 
    try { 
     return new BufferedReader(new InputStreamReader(System.in)).readLine(); 
    } catch (IOException e) { 
     out.println("Error reading file!"); 
    } 
    return enterNameFileConsole(); 
} 

String removeSpacesDisplaysContents() { 
    try { 
     return deleteWhitespace(new BufferedReader(new InputStreamReader(file)).readLine()); 
    } catch (IOException e) { 
     out.println("Error reading file!"); 
    } 
    return removeSpacesDisplaysContents(); 
} 

私は、App()、enterNameFileConsole()とremoveSpacesDisplaysContents()テストする必要があります。

誰かが提示したり説明したり、アイデアがある場合は、Mockitoを使用してメソッドと条件をテストする方法。

トピックが繰り返されると助けて、ごめんなさい。

+2

'removeSpacesDisplaysContents'メソッドは' return removeSpacesDisplaysContents(); 'の無限再帰的ループで自身を呼び出しているので、あなたのプログラムには無限の再帰的ループがあります。 – Jesper

+0

私はそれを変更しようとしましたが、入力ストリームに問題があり、ストアのパスにファイルの名前を与えませんでした。代わりに、try ... cryを使用します。 – Ziomell

+0

まず、Javaでのプログラミングの基礎を学ぶことに専念し、Mockitoなどの複雑なものを使う前に、クラスとメソッドの仕組みを完全に理解しておく必要があります。 – Jesper

答えて

1

ファイルシステムまたはコマンドラインとの相互作用は、直接テストするのが難しいです。私は通常それらを別のクラスまたはメソッドに抽出し、そのクラス/メソッドの動作をスタブしてテストします。

import java.io.IOException; 
import java.io.PrintStream; 

public class App { 

    private PrintStream out; 
    private InputReader inputReader; 

    public App() { 
     this(System.out, new InputReader()); 
    } 

    // constructor injection used by tests 
    public App(PrintStream out, InputReader inputReader) { 
     this.out = out; 
     this.inputReader = inputReader; 
    } 

    public void execute() throws IOException { 
     if (inputReader.determineFile()) { 
      out.println(inputReader.removeSpacesDisplaysContents()); 
     } else { 
      out.println("No File!"); 
     } 
    } 


    public static void main(String[] args) throws IOException { 
     App siema = new App(); 
     siema.execute(); 
    } 

} 

と:Appの

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import static java.lang.System.out; 

public class InputReader { 

    private InputStream in; 
    private InputStream file; 

    public InputReader() { 
     this(System.in); 
    } 

    // constructor injection used by tests 
    public InputReader(InputStream in) { 
     this.in = in; 
    } 

    public boolean determineFile() { 
     out.println("Enter filename:"); 
     try { 
      file = getResource("/" + readLine(in)); 
      return true; 
     } catch (IOException e) { 
      out.println("Error determining file!"); 
      return false; 
     } 
    } 

    public String removeSpacesDisplaysContents() throws IOException { 
     return deleteWhitespace(readLine(file)); 
    } 

    private String deleteWhitespace(String input) { 
     return input.replaceAll("\\s+", ""); 
    } 

    // to be overridden in tests 
    InputStream getResource(String name) throws IOException { 
     return getClass().getResourceAsStream(name); 
    } 

    // to be overridden in tests 
    String readLine(InputStream is) throws IOException { 
     return new BufferedReader(new InputStreamReader(is)).readLine(); 
    } 

} 

試験:例えば

import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.Mock; 
import org.mockito.runners.MockitoJUnitRunner; 

import java.io.IOException; 
import java.io.PrintStream; 

import static org.mockito.Mockito.verify; 
import static org.mockito.Mockito.when; 

@RunWith(MockitoJUnitRunner.class) 
public class AppTest { 

    private App instance; 
    @Mock 
    private PrintStream out; 
    @Mock 
    private InputReader inputReader; 

    @Before 
    public void setUp() { 
     instance = new App(out, inputReader); 
    } 

    @Test 
    public void testExecute() throws IOException { 
     //SETUP 
     when(inputReader.determineFile()).thenReturn(true); 

     String expectedResult = "test result"; 
     when(inputReader.removeSpacesDisplaysContents()).thenReturn(expectedResult); 

     // CALL 
     instance.execute(); 

     // VERIFY 
     verify(out).println(expectedResult); 
    } 

    @Test 
    public void testExecuteCannotDetermineFile() throws IOException { 

     // SETUP 
     when(inputReader.determineFile()).thenReturn(false); 

     // CALL 
     instance.execute(); 

     // VERIFY 
     verify(out).println("No File!"); 
    } 
} 

そしてInputReaderための試験:

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.Mock; 
import org.mockito.runners.MockitoJUnitRunner; 

import java.io.IOException; 
import java.io.InputStream; 

import static junit.framework.TestCase.assertFalse; 
import static junit.framework.TestCase.assertTrue; 
import static org.testng.AssertJUnit.assertEquals; 

@RunWith(MockitoJUnitRunner.class) 
public class InputReaderTest { 

    @Mock 
    private InputStream in; 

    @Test 
    public void testDetermineFile() { 
     // SETUP 
     InputReader instance = new InputReader(in) { 

      @Override 
      InputStream getResource(String name) { 
       return null; 
      } 

      @Override 
      String readLine(InputStream is) throws IOException { 
       return null; 
      } 
     }; 

     // CALL 
     boolean result = instance.determineFile(); 

     // VERIFY 
     assertTrue(result); 
    } 

    @Test 
    public void testDetermineFileError() { 
     // SETUP 
     InputReader instance = new InputReader(in) { 

      @Override 
      InputStream getResource(String name) throws IOException { 
       return null; 
      } 

      @Override 
      String readLine(InputStream is) throws IOException { 
       throw new IOException(); 
      } 
     }; 

     // CALL 
     boolean result = instance.determineFile(); 

     // VERIFY 
     assertFalse(result); 
    } 

    @Test 
    public void testRemoveSpacesDisplaysContents() throws IOException { 
     // SETUP 
     final String line = "test result"; 
     String expectedResult = "testresult"; 
     InputReader instance = new InputReader(in) { 

      @Override 
      InputStream getResource(String name) throws IOException { 
       return null; 
      } 

      @Override 
      String readLine(InputStream is) throws IOException { 
       return line; 
      } 
     }; 

     // CALL 
     String result = instance.removeSpacesDisplaysContents(); 

     // VERIFY 
     assertEquals(expectedResult, result); 
    } 

    // the test succeeds if an IOException is thrown 
    @Test(expected = IOException.class) 
    public void testRemoveSpacesDisplaysContentsError() throws IOException { 
     // SETUP 
     InputReader instance = new InputReader(in) { 

      @Override 
      InputStream getResource(String name) throws IOException { 
       return null; 
      } 

      @Override 
      String readLine(InputStream is) throws IOException { 
       throw new IOException(); 
      } 
     }; 

     // CALL 
     instance.removeSpacesDisplaysContents(); 
    } 
} 
+0

Adriaan Kosterは、以前の問題を自分でアドバイスしました。問題は今、私が正しいファイル名に与えると、 "No File"の代わりに誤った表示エラーを出すので、内容を表示するということです。 それはIfのprolemになることができますか? – Ziomell

+0

InputReaderTestのコードを更新しました。モックが見つかりませんでした。あなたが説明しようとしている問題は、アプリケーションを実行しているとき、またはテストを実行しているときに起こりますか?私はあなたのコードを壊しているかもしれません、私はコンパイルしたりテストしたりしませんでした。何が間違っているのかを明確にしてください。 –

+0

私はメールであなたに、メールはウェブサイトで見つけました。私はこの問題について説明し、スクリーンショットに参加しました。ここで私は問題を説明できませんでした。どうもありがとうございました – Ziomell

関連する問題