dsot
dsot

Reputation: 21

creating unit testing for write to file method in junit.

I am trying to write test methods in Intellij with jUnit. I can succesfully write to the file, however, I need to show that I can write a test method for this. I push in the write direction would be great. My ServerController class write to a file, checks a file for a username, and verifies username and password. This project is for a class and it appears that the most important lesson is learning about documentation (requirements, design, and testing). So, here I am trying to test.

package appLayer;

import java.io.*;
import java.util.Scanner;

public class ServerController {
    public void writetoFile(String writeUsername, String writePassword) throws IOException {
        PrintWriter fileWriting = new PrintWriter(new FileOutputStream("/Users/dannielsotelo/Documents/database.txt", true));
        fileWriting.println(writeUsername + "," + writePassword); //
        System.out.println(writeUsername + " was saved to database.txt");
        fileWriting.close();
    }

    public boolean findUsername(String fUsername) {
        File file = new File("/Users/dannielsotelo/Documents/database.txt");
        try{
            Scanner scanner = new Scanner(file);
            int lineNum = 0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                lineNum++;
                if(line.contains(fUsername))
                    return true;
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        }
        return false;
    }

    public boolean verifyCredentials(String lUsername, String lPassword) {
        File file = new File("/Users/dannielsotelo/Documents/database.txt");
        try{
            Scanner scanner = new Scanner(file);
            int lineNum = 0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                lineNum++;
                if(line.contains(lUsername) && line.contains(lPassword))
                    return true;
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        }
        return false;
    }
}

and my ServerControllerTest class

package appLayer;

import org.junit.Test;


import static org.junit.Assert.*;

public class ServerControllerTest {
    @Test
    public void testwritetoFile() {
    }

    @Test
    public void findUsername() {
    }

    @Test
    public void verifyCredentials() {


    }
}

Upvotes: 0

Views: 4148

Answers (2)

Jan B.
Jan B.

Reputation: 6450

First, you should get rid of the redundant code when scanning the files. It would be sufficient to have one method that searches for arbitrary strings since technically it doesn't matter if you search for a username or a password.

Second, for testing purposes you really should not hard-code the database file. When writing JUnit tests you don't want your actual file to be written or read. Pass the file or the path to it as a constructor parameter.

Writing a test method is quite straight-forward. Write a value to the database, then read it and check if the previously written data is there. It's a good idea to start with a blank file for every test.

PS.: In terms of security, the approach of checking if a line contains username and password is a little disaster :D

Upvotes: 1

J.Doe
J.Doe

Reputation: 299

Search this forum for testing void methods. But anyway your code is not really testable in sensible way. While you can test methods that return some value, you don't give it enough starting conditions to be sure they do what you want. Don't hardcode path to files in the method, you'll be better off if it's an argument to the method. Then in tests you can be sure that if you create file with a specified content the method will do this or that or throw an exception if you don't create the file at all.

Upvotes: 0

Related Questions