0

I need to use JUnit to test a few methods I have written. When I go to create a test class and write a test method, IntelliJ runs the entire program (including the main method) which is not finished. I want only to test a single method and for that, I've done as the instructions say (Ctrl+Shift+F10) or the little green Play button near the method if you will. Still, It always runs the entire code and gives me errors not regarding the method I want to test.

class QueensTest {

    @Test
    void testCreate() {
        assertNotNull(Queens.create(4));
        assertThrows(IllegalArgumentException.class, () -> Queens.create(-1));
    }
}

This is the method I want to test, and it gives me this error.

java: cannot find symbol
  symbol:   variable TODO
  location: class at.jku.ssw.queens.app.QueensMain

If it would help I could post the entire code (all of the classes).

The code that causes the error:

public static void main(String[] args) throws Exception {
    
    Out.print("Waehle Groesse: ");
    int n = In.readInt();
    Queens game = TODO;

    Out.println(); 
    printBoard(game);
    
    boolean terminate = false; 
    while (TODO) {
        Out.println(); 
        Out.print("Waehle Setzen (S) | Entfernen (E) | Clear (C) | Quit (Q): ");
        char cmd = Character.toUpperCase(In.readChar()); 
        if (cmd == 'S') {
            Out.print("Setze Queen auf Zeile: "); 
            int row = In.readInt(); 
            Out.print("                Spalte: "); 
            char col = Character.toUpperCase(In.readChar());

            // TODO

        } else if (cmd == 'E') {
            Out.print("Entferne Queen von Zeile: "); 
            int row = In.readInt();
            Out.print("                   Spalte: "); 
            char col = Character.toUpperCase(In.readChar());

            // TODO

        } else if (cmd == 'C') {

            // TODO

        } else if (cmd == 'Q') {
            terminate = true;
        }   else {
            Out.println("Ungueltiges Kommando!");
        }
    }
}

private static void printBoard(Queens game) throws Exception {
    Out.print("   |");
    for (int col = 0; col < game.getSize(); col++) {
        Out.print(String.format(" %c ", (char)('A' + col)));
    }
    Out.println("|"); 
    Out.print("---|");
    for (int col = 0; col < game.getSize(); col++) {
        Out.print("---"); 
    }
    Out.println("|");

    for (int row = 1; row <= game.getSize(); row++) {
        Out.print(String.format(" %d |", row));
        for (int col = 0; col < game.getSize(); col++) {
            Out.print(TODO ? " Q " : " . ");
        }
        Out.println("|"); 
    }
    Out.print("---|");
    for (int col = 0; col < game.getSize(); col++) {
        Out.print("---"); 
    }
    Out.println("|");  
    Out.println(); 
    Out.println("Stand: " + TODO);

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
codeisfun
  • 187
  • 11
  • 1
    It would help if you were to show the code that causes the compilation error. – Turing85 Jun 05 '21 at 16:23
  • 1
    I doesn't "run the entire code", it simply tries to *compile* it! You don't need to finish your code fully, just do enough for it to compile successfully (some strategically placed `throw new UnsupportedOperationException("not yet implemented");` can help with that). – Joachim Sauer Jun 05 '21 at 16:26
  • Okay that is something I can try but what baffels me is what other people had 0 problems with this. – codeisfun Jun 05 '21 at 16:31
  • 1
    It doesn't **run** all your code, it has to **compile** all your code, and that compilation fails. – Mark Rotteveel Jun 05 '21 at 16:33
  • Okay lets say if I would comment out the parts where the error appers,meaning the lines and the code compiles the testing should work fine? Will the comments in the main class affect the test methods or are they "independent" from one another? – codeisfun Jun 05 '21 at 16:35

0 Answers0