2

I am currently working on this program the method takes a queue as a parameter. Then it sorts the queue in ascending order and returns it. It's working fine however I don't know how to handle an empty queue. I tried to print out "Invalid Input" but the test class is showing an error. The "Invalid Input" code block keeps executing even if an empty queue is not passed. So none of the tests are passing. I really need your help!

Code for sorting:

import java.util.Queue;

public class Sort {

    public static Queue<Integer> queueSort(Queue<Integer> queue) { 
                                                            
        int n = queue.size(); 

        if (queue.isEmpty()) {      
            System.out.println("Invalid Input");

        } else {

            for (int i = 0; i < n; i++) {  

                int minimumIndex = -1;
                int minimumValue = Integer.MAX_VALUE; 
                                                      

                for (int j = 0; j < n; j++) {        
                    int currentValue = queue.poll();

                    if (currentValue < minimumValue && j < (n - i)) {  
                        minimumValue = currentValue;                  
                        minimumIndex = j;            
                    }                                
                    queue.add(currentValue);        
                }

                for (int j = 0; j < n; j++) {     
                    int currentValue = queue.poll();

                    if (j != minimumIndex) {
                        queue.add(currentValue);
                    }
                }

                queue.add(minimumValue);      
            }

        }
        return queue;
    }
}

Test class:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.LinkedList;
import java.util.Queue;

class SortTest {

    Queue<Integer> Q1 = new LinkedList<>();
    Queue<Integer> expectedQ1 = new LinkedList<>();

    Queue<Integer> Q2 = new LinkedList<>();
    Queue<Integer> expectedQ2 = new LinkedList<>();

    Queue<Integer> Q3 = new LinkedList<>();

    void testInputs() {
        Q1.add(10); Q1.add(-1); Q1.add(8); Q1.add(7); Q1.add(2);
        expectedQ1.add(-1); expectedQ1.add(2); expectedQ1.add(7); expectedQ1.add(8); expectedQ1.add(10);

        Q2.add(6000); Q2.add(5999);
        expectedQ2.add(5999); expectedQ2.add(6000);

    }


    @Test
    void queueSortTest1() {
        Assertions.assertEquals(expectedQ1, Sort.queueSort(Q1));
    }

    @Test
    void queueSortTest2() {
        Assertions.assertEquals(expectedQ2, Sort.queueSort(Q2));
    }

    @Test
    void queueSortTest3() {
        Assertions.assertEquals("Invalid Input", Sort.queueSort(Q3));
    }
}
Sk_Hssn
  • 55
  • 6

2 Answers2

0

The assertion is checking the returned value. In case the queue is empty you are printing "Invalid Input" not returning this

if (queue.isEmpty()) {      
    System.out.println("Invalid Input");
}

The returned value is empty queue in this case and it's not equal to string "Invalid Input"

Therefore you need to change your business logic if there is such requirement (e.g. to throw an exception) - of course do not change your logic to satisfy test! :)

The other way is to test the output, for example JUnit test for System.out.println()

m.antkowicz
  • 11,566
  • 12
  • 33
0

If you want to assert a System.out, you should intercept it.

That's an example:

@Test
void queueSort() {
    final PrintStream standardOut = System.out;
    final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outputStreamCaptor));
    Sort.queueSort(Q3)
    Assertions.assertEquals("Invalid Input", outputStreamCaptor.toString().trim());
}

There's a good article on Baeldung about it.

Eduard Dubilyer
  • 841
  • 2
  • 8
  • 15