vendredi 26 décembre 2014

How to allow this login class to pass junit testing without errors?

In the assignment I am working on I have to test my code using junit plugin in netbeans. my code consists of too many classes that depends on each other.


I just need to know how to do it for one of these classes to be able to apply the same logic on the rest of the classes.


So, here is the login class that I want to test:



package StateMachine;

import Objs.*;
import main.promptFactory;

/**
*
* this class asks the logged in user for its username to check its validity and
* refers to its object in the loggedInUser variable in the motherMachine.
* If user is validated, it changes myState to an object of idle.
*
*/
public class login implements stateInterface{

@Override
public void processCode(MotherMachine m, int code) {

String name = promptFactory.promptString("Please enter your username to login:");
try{
int i = Integer.parseInt(name);

if ((i == 00) || (i == 01) ||(i == 02) ||(i == 03) ||(i == 04) ||(i == 05) ||(i == 06)){
System.out.println("Log in before asking for any transaction");
}}
catch(NumberFormatException ex){
userObj newuser = IO.users.search(name); //make this true if user is valid using above code

if (newuser != null){ //if user is valid, initiate session
m.loggedInUser = newuser;
main.test.code = -2;
m.setState(new idle());


}
else if (newuser == null){ //if name is invalid
System.out.println("User does not exist"); //don't accept, prompt again
}
}


}

}


this class implements the following state interface:



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package StateMachine;

/**
*An interface for the states to implement.
*/
public interface stateInterface {
public void processCode(MotherMachine m, int code);
}


This is the MotherMachine class that has myState and LoggedInuser variables as well as the setState method and the processCode method:



package StateMachine;

import Objs.userObj;

/**
*
This package is the thinking brain behind the whole system. The event-driven design traversers between the classes based on
the input (transaction code).
* These classes take in the input from user to process it.
*
MotherMachine.java: This class is the starting point of the program, the transaction code inputted is passed to the
* current (myState) object. [Could be to the login class, or create or delete or or or..]

*
*/
public class MotherMachine{

private stateInterface myState;
public userObj loggedInUser = new userObj();

public MotherMachine(){
this.setState(new login()); //initial state is alway login (the default)
}

public void setState(stateInterface newState){
myState = newState;
}
public void processCode(int code){
// System.out.println(out);
if((code <= 06 || code >= 00) && !(myState instanceof login)){ //if a session is in order and a valid transaction is submitted
myState.processCode(this, code); //the current state will recieve the code

}
else if (code == -1 && (myState instanceof login)){ //if a session hasn't yet started and the login code has been submitted
myState.processCode(this, code); //pass code to the current state (will be login)
/////////////////////GO TO LOGIN CLASS//////////////////
}
else {
System.out.println("Invalid transaction code");
}
}
}


the userObj class contains the objects needed by any users and heavily used while login.


and the promptfactory class Factory is just to prompt user for integers,double, string, or chars.


Please I need a detailed answer for this to understand exactly what to do and to be able to apply it on the rest of the classes. ( for junitTest classes / stubs and anything else that can be used)


Appreciating your help.


Thank you ..


Aucun commentaire:

Enregistrer un commentaire