This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Testing and the fly likes Junit testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit testing " Watch "Junit testing " New topic
Author

Junit testing

Jean Stu
Greenhorn

Joined: Mar 13, 2014
Posts: 14
im trying to test a class 'generator' which gives a 9x9 suduko puzzle. firstly a user puts in a number of how many blank squares it want. then it generates

this is the generator class


as it generates a random puzzle each time, how can i test it?



i hope this is enough to help
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Jean,
A Sudoku puzzle has certain rules. In particular, no duplicate #s in the same row, column or box. You can use asserts (in loops) to confirm the randomly generated puzzle meets all this criteria. You can also call instance.createRandomPuzzle twice to ensure you get a different puzzle across calls. It is theoretically possible to get the same puzzle twice in a rows so I make multiple calls when I do this to decrease that chance to an acceptable level. (There's enough Sudoku puzzles that you are starting at an acceptable level though.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jean Stu
Greenhorn

Joined: Mar 13, 2014
Posts: 14
Jeanne Boyarsky wrote:Jean,
A Sudoku puzzle has certain rules. In particular, no duplicate #s in the same row, column or box. You can use asserts (in loops) to confirm the randomly generated puzzle meets all this criteria. You can also call instance.createRandomPuzzle twice to ensure you get a different puzzle across calls. It is theoretically possible to get the same puzzle twice in a rows so I make multiple calls when I do this to decrease that chance to an acceptable level. (There's enough Sudoku puzzles that you are starting at an acceptable level though.)


thanks
so what im initailly trying is to test if numbers are unique ie no duplicates in row,


i get the following error, expected: <1,2,3,4,5,6,7,8,9> but was: </* this was the full suduko puzzle*/> how can i get it so it passes
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

This is looping through all the rows and columns.


You want to do this one row at a time and one column at a time separately. This handles one of them. Note the assert is inside the loop. You might consider using a Set rather than a List. Then you don't need the contains and can just add integers to your set.



Handling one column at a time is similar, but you change the index order.
Jean Stu
Greenhorn

Joined: Mar 13, 2014
Posts: 14
Jeanne Boyarsky wrote:


ive added the code as followed:


it now gives the error; expected:<[1,2,3,4,5,6,7,8,9]> but was: <[single number that keeps changing after each test run]> ie <[2]> or <[6]>
it also gives the same error if i do
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

And this is why one (me) should always test code. The code I had provided created a new list of size one and called assert which would never work. This one should go better.

Jean Stu
Greenhorn

Joined: Mar 13, 2014
Posts: 14
Jeanne Boyarsky wrote:And this is why one (me) should always test code. The code I had provided created a new list of size one and called assert which would never work. This one should go better.



when added it produces the error

expected: <[1,2,3,4,5,6,7,8,9]> but was: <[2,1,5,4,6,3,9,7,8]>

all it is, is in a different order
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Jean Stu wrote:all it is, is in a different order

See my suggestion about using Set so your test isn't dependent on order. Or you can call Collections.sort() on the list before the assert.
Jean Stu
Greenhorn

Joined: Mar 13, 2014
Posts: 14
Jeanne Boyarsky wrote:
See my suggestion about using Set so your test isn't dependent on order. Or you can call Collections.sort() on the list before the assert.


ive used

but get this error
java.lang.Integer cannot be cast to java.lang.Character
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Junit testing
 
Similar Threads
struct in java
Threads with static variables
here is a question and i have a very similar code but i need to convert it
toString() on Integer class
Sudoku Solver