简体   繁体   中英

One method fails unit test only when all tests are run but passes when tested individually?

I am a first year at college studying application dev and our assignment is to make a simple registration and log in system. The only problem right now is that the unit testing doesn't work when I run them all at once with the Run Tests feature on VSCode. the error is this

java.lang.AssertionError
 at st10035771.LoginTest.testIsValidPasswordF(LoginTest.java:35)
 at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
 at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
 at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
 at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
 at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
 at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
 at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
 at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
 at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
 at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)

Login.java:

    package st10035771;


import java.util.Scanner;

public class Login {
    public final static int USERNAME_LIMIT = 5;
    public final static int PASSWORD_LENGTH = 8;
     

    public static boolean checkUserName(String username) {
        return username.length() <= USERNAME_LIMIT && username.contains("_");

    }
/*
 * code attribute
 * this code was adapted from DelftStack
 * https://www.delftstack.com/howto/java/password-checker-java/
 */
    public static boolean checkPasswordComplexity(String password) {
      boolean isValidPassword;
      final int minUppers = 1;
      final int minDigits = 1;
      final int minSpecials = 1;
      int uppers = 0;
      int digits = 0;
      int specials = 0;

        for (int i = 0; i < password.length(); i++) {
            char ch = password.charAt(i);
            if (Character.isUpperCase(ch))
                uppers++;
            else if (Character.isDigit(ch))
                digits++;
            //if (ch >= 33 && ch <= 47 || ch == 64) {
              if (!Character.isLetterOrDigit(ch)) {
                specials++;
            }

        }

        if (password.length() >= PASSWORD_LENGTH && uppers >= minUppers && digits >= minDigits
                && specials >= minSpecials) {

            return isValidPassword = true;
        } else {
            return isValidPassword = false;
        }

    }
    public static void inputUser(){
        System.out.println("Please enter username:");
         Scanner un = new Scanner(System.in);
            String userName = un.next();

            if (checkUserName(userName)) {
                System.out.println("Username successfully captured");
            } else {
                System.out.println(
                        "Username is not correctly formatted, please ensure that your username contains an underscore and is no more than 5 characters in length.");
            }
    }

    

    public static void inputPass(){
        System.out.println("Please enter password:");
         Scanner pa = new Scanner(System.in);
            String userPass = pa.next();

            if (checkPasswordComplexity(userPass)) {
                System.out.println("Password successfully captured");
            } else {
                System.out.println(
                        "Password is not correctly formatted, please ensure that the password contains at least 8 characters, a capital letter, a number and a special character.");
            }
        
    }

    
}

LoginTest.java

package st10035771;

import org.junit.Test;
import static org.junit.Assert.*;

public class LoginTest {

    public LoginTest() {
    }

    Login loginTest = new Login();

    @Test
    public void testSomeMethod() {

    }

    @Test
    public void testCheckUserName() {
        assertTrue(loginTest.checkUserName("kyl_1"));
    }

    @Test
    public void testCheckUserNameF() {
        assertFalse(loginTest.checkUserName("kyle!!!!!!!"));
    }

    @Test
    public void testIsValidPassword() {
        assertTrue(loginTest.checkPasswordComplexity("Ch&&sec@ke99!"));
    }

    @Test
    public void testIsValidPasswordF() {
        assertFalse(loginTest.checkPasswordComplexity("password"));
    }

}

The only one that fails when tested all at once is the TestIsValidPasswordF

Main issue is your uppers / digits / specials counts are never set back to zero after checking a password. So when you run all tests and it gets to testIsValidPasswordF I would assume testIsValidPassword has ran first and you already have a number greater than 1 in these counts.

When you run a test on its own the counts start as 0 which is why it passes.

Moving these counts to the start of the checkPasswordComplexity method should fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM