Home > Back-end >  How to create MockObject unit test?
How to create MockObject unit test?

Time:11-10

Please check the following code, an infinite loop, ASK for input VALID values, but we don't give
Need to create MockObject can read AskCode. Otherwise, the user must manually input data, then, must check if it is valid or must request again,
Question:
Mock Objects
The user can participate in the test? (scan data)
How can we make the assertEquals is used in the data array?

 
Package MasterMindTQSProject;

The import static org. Junit. Assert. *;

The import org. Junit. Test;

Public class GameTest {
MockMyScanner sc=new MockMyScanner ();
MyScanner NSC=new MyScanner ();
Game Game=new Game (sc);

@ Test
Public void testAskNumTriesDefault () {
Int testTries=10;
AssertEquals (testTries, game getTries ());
}

@ Test
Public void testAskNumTriesCustomized () {
Int testTries=15;
Game. Sc. SetNewInt (15);
Game. ProxyAskTries ();
AssertEquals (testTries, game getTries ());
}

@ Test
Public void testSelectModeDefault () {
Int testLengthTest=5;
Int testMinValue=https://bbs.csdn.net/topics/1;
Int testTries=10;

Game. Sc. SetNewInt (1);//Game mode

Game. SelectMode ();
AssertEquals (testLengthTest, game. SecretWord. GetWord_length ());
AssertEquals (testMinValue, game. SecretWord. GetMin_number ());
AssertEquals (testTries, game getTries ());
}

@ Test
Public void testSelectModeCustomized () {
Int testLengthTest=4;
Int testMinValue=https://bbs.csdn.net/topics/5;
Int testTries=15;

Game. Sc. SetNewInt (2);//Game mode
Game. Sc. SetNewInt (testLengthTest);//WordLength
Game. Sc. SetNewInt (testMinValue);//MinValue
Game. Sc. SetNewInt (testTries);//Tries
Game. SelectMode ();

AssertEquals (testLengthTest, game. SecretWord. GetWord_length ());
AssertEquals (testMinValue, game. SecretWord. GetMin_number ());
AssertEquals (testTries, game getTries ());
}

//@ Test
//we found an infinite loop, because ASK for input VALID values, but we don't give them
/* * need to create MockObject can read AskCode, otherwise, the user must manually input data, then, must check if it is valid or must request again,
Question:
Mock Objects
The user can participate in the test? (scan data)
How can we make the assertEquals is used in the data array? */

Public void testInsertCode () {
Int [] testCode={1, 2, 3, 4, 5};
Int [] testCodeError1=,2,8,4,5 {1};
Int [] testCodeError2=,2,3,4,5,4 {1};
Int [] testCodeError3={1, 2, 3, 4};
Int [] testCodeError4={1, 2, 3, 4, 9};


String mockCode="1 2 3 4 5";
Game. Sc. SetNewCode (mockCode);
Game. AskCode ();
for (int i=0; I & lt; 5; I++) {
AssertEquals (testCode [I], game getCodeTry () [I]);
}

8 mockCode="1, 2, 4, 5".
Game. Sc. SetNewCode (mockCode);
Game. AskCode ();
for (int i=0; I & lt; 5; I++) {
AssertNotEquals (testCode [I], game getCodeTry () [I]);
}

MockCode="1, 2, 3, 4, 5 4";
Game. Sc. SetNewCode (mockCode);
Game. AskCode ();
for (int i=0; I & lt; 5; I++) {
AssertNotEquals (testCode [I], game getCodeTry () [I]);
}


MockCode="1 2 3 4";
Game. Sc. SetNewCode (mockCode);
Game. AskCode ();
for (int i=0; I & lt; 5; I++) {
AssertNotEquals (testCode [I], game getCodeTry () [I]);
}

MockCode="1, 2, 3, 4-9";
Game. Sc. SetNewCode (mockCode);
Game. AskCode ();
for (int i=0; I & lt; 5; I++) {
AssertNotEquals (testCode [I], game getCodeTry () [I]);
}
}

/*//@ Test
Public void testInsertCodeRangeDefault () {
Int codeOK []=new int [] {1, 2, 3, 4, 5};
Int codeNotOKLow []=new int [] {6};
Int codeNotOKHigh []=new int [] {6};
Game. CodeInRange (code);
AssertEquals (code. The size, game. SecretWord. GetWord_length ());
}

//@ Test
Public void testInsertCodeRangeCustomized () {
Game. CodeInRange ();
AssertEquals (code. The size, game. SecretWord. GetWord_length ());
}

//@ Test
Public void testInsertCodeLengthDefault () {
Int codeOK []=new int [] {1, 2, 3, 4, 5};
Int codeNotOKLow []=new int [] {6};
Int codeNotOKHigh []=new int [] {6};
Game. CodeInRange (code);
AssertEquals (code. The size, game. SecretWord. GetWord_length ());
}

//@ Test
Public void testInsertCodeCustomized () {
Int codeOK []=new int [] {1, 2, 3, 4, 5};
Int codeNotOKLow []=new int [] {6};
Int codeNotOKHigh []=new int [] {6};
Game. CodeInRange (code);
AssertEquals (code. The size, game. SecretWord. GetWord_length ());
} */


@ Test
Public void testCorrectPosition () {
Int correctPosition.
String testCode="1 2 3 4 5";

Game. Sc. SetNewCode (testCode);
Game. AskCode ();
Game. SecretWord. SetMin_number (1);
Game. SecretWord. SetWord_length (5);
Int [] gameCode={1, 2, 3, 4, 5};
Game. SecretWord. SetSecretWord (gameCode);

CorrectPosition=game. GetNumbersCorrectPosition ();
AssertEquals (5, correctPosition);


Int [] gameCode2=,2,3,4,4 {1};
Game. SecretWord. SetSecretWord (gameCode2);

CorrectPosition=game. GetNumbersCorrectPosition ();
AssertEquals (4, correctPosition); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related