Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

android - How to mock Scanner scanner = new Scanner(file, "UTF-8") in Roboelectric

I am facing an issue to mock below mention line using Roboelectric or Powermockito

File file = new File(context.getFilesDir() + FILE_NAME);
Scanner scanner = new Scanner(file, "UTF-8")

it throws me the below exception java.io.FileNotFoundException: "data/data/Logs/Text.txt" (The system cannot find the file specified)

Even though I was able to mock the file and set the path to the file.

Any suggestion on how to mock the scanner class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Got the solution for this, I just created the file in the resource folder with the same file name and added dummy text to it. So that when the scanner statement gets executed in the test case it will read the dummy file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...