📋
Auto-Grading for Teachers
  • Learn How to Auto-Grade
  • Assertions and Unit Testing
  • Request / Submit Examples
  • 🐍Python Examples
    • Check Variable Values
    • Output Tests
    • Input / Output Tests
    • Print Function Tests
    • Return Function Tests
    • Random Number Tests
    • Parse Student Code
  • ☕Java Examples
    • Check Variable Values
    • Output Tests
    • Input / Output Tests
    • Return Method Tests
    • Class - .toString() and Void Method Tests
    • Random Numbers Tests
    • Array and ArrayList Tests
    • Parse Student Code
  • 💎Ruby Examples
    • Output Tests
    • Input / Output Tests
    • Print Function Tests
    • Return Function Tests
    • Parse Student Code
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Java Examples

Parse Student Code

Often you just want to make sure students successfully used a particular function/command/structure. The simplest way I have found to do this is just parse the student's code file and make sure what you are looking for is there.

For example, maybe you want to make sure the student used println() at least 2 times. We can parse the student's code file and count how many times "System.out.println(" is present in their code.

This test assumes the student's file name is Example.java.

import org.junit.Test;
import static org.junit.Assert.*;
import java.nio.file.*;

public class CodingRoomsUnitTests {

    @Test
    public void testDefaultCase() throws Exception {
        int count = 0;
        String student_code = new String(Files.readAllBytes(Paths.get("Example.java")));
        String[] words = student_code.split(" ");

        for (int i = 0; i < words.length; i++) {
            if (words[i].indexOf("System.out.println(") >= 0) {
                count++;
            }
        }
        assertTrue(count >= 2);
    }
}
PreviousArray and ArrayList TestsNextOutput Tests

Last updated 3 years ago

Was this helpful?

☕