Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I create a Junit test case using Java code that test logging functionalit

ID: 3909427 • Letter: H

Question

How do I create a Junit test case using Java code that test logging functionality to the Log.txt file? Please explain the test case implemented.

Here is the code used:

    //this method inputs username and password in login application
    private void LogsubmitActionPerformed(java.awt.event.ActionEvent evt) {                                        
    
        try {
            String password = jPassword.getText();
            String username = jtxtUsername.getText();
          
            File texting = new File("Log.txt");
            //open file in append mode
            BufferedWriter writer;
            writer = new BufferedWriter(new FileWriter(texting,true));
            //date and time
            Date currentTime =Calendar.getInstance().getTime();
            boolean valid = false;
            //used a hard-coded username and password
            if(password.equals("password")&&(username.equals("anna")))
            {
                jtxtUsername.setText(null);
                jPassword.setText(null);
              
                JOptionPane.showMessageDialog(null, "success");
                writer.write(username + currentTime);
                writer.write("success");
                valid = true;
          
            }
            else if(!valid)
          
            JOptionPane.showMessageDialog(null, "invalid password");
            writer.write(username + currentTime);
            writer.write("failed");
            writer.close();
          
          
        } catch (IOException ex) {
            Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, ex);
        }
                                            
    }

Explanation / Answer

Hello Sir/Madam

Here username and password is given in login application

We will get the username and password from the login application using the following two lines of code

String password = jPassword.getText();

String username = jtxtUsername.getText();

File texting = new File("Log.txt");

In above File object is created by passing in a String that represents the name of a file

BufferedWriter writer=new BufferedWriter(new FileWriter(texting,true));
Here we will create the buffered class object.

Date currentTime =Calendar.getInstance().getTime();...

Here we will get the current Date and time using the above line

if(password.equals("password")&&(username.equals("anna")))
            {
                jtxtUsername.setText(null);
                jPassword.setText(null);
              
                JOptionPane.showMessageDialog(null, "success");
                writer.write(username + currentTime);
                writer.write("success");
                valid = true;
          
            }
            else if(!valid)
          
            JOptionPane.showMessageDialog(null, "invalid password");
            writer.write(username + currentTime);
            writer.write("failed");
            writer.close();

}

Above we have written a if-else loop which will check the whether the given username password is correct or not if the username and password is correct then it will shows the dialog message showing that the password is correct,whereas if the username and password is not correct then it will shows the dialog message showing that the password is not correct..

catch (IOException ex) {
            Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, ex);
        }

This line of code throws if any Input/Output Exceptions araises

Hope It works and looking forward to help if have any doubts..

Thank You Sir/Madam.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote