Java unit test for void methods

First of all, I would like to apologize for my language skills, but my mother tongue is not English.

I have a small source code:

public void hello(List l){    
    switch (l.get(0) {    
    case "hello":    
         System.out.println("Hello!");    
    case "world":    
        System.out.println("World");    
        break;    
    }    
}

And I have to write a unit test that can test this code. But my skill is not enough. Can someone help me please? An example or a book or something else. I do not know how I can start it!

I value.

thank

+3
source share
5 answers

, , , "". , - . , , , .

+4

: , .

+2

, JVM , , , ...

, . , String, -, , .

+2

, , System.out. , System.out , , . , .

, System.out . PrintStream, , , PrintStream, .

+1

, ( Mockito).

PrintStream mockStream = Mockito.mock(PrintStream.class);
PrintStream original = System.out;
System.out = mockStream;

//call your method here:
hello(myList);

//check if your method has done its job correctly
Mockito.verify(mockStream).println("Hello!");
//clean up
System.out = orig;

, break , .

+1

All Articles