├── .gitignore ├── README.md ├── build.gradle └── src ├── main └── java │ └── com │ └── example │ └── project │ └── Hello.java └── test └── java └── com └── example └── project └── TestHello.java /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .gradle/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Autograding Example: Java 2 | This example project is written in Java, and tested with Gradle/JUnit. 3 | 4 | ### The assignment 5 | The tests are currently failing because of an output mismatch. Fixing the `System.out.println` in the main method will make the tests green. 6 | 7 | ### Setup command 8 | N/A 9 | 10 | ### Run command 11 | `gradle test` 12 | 13 | ### Notes 14 | - The JDK is installed on GitHub Actions machines, so you're also able to directly invoke `javac`, `java`, or any other CLI command included in the JDK. 15 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | repositories { 6 | mavenCentral() 7 | } 8 | 9 | dependencies { 10 | testImplementation('org.junit.jupiter:junit-jupiter:5.6.0') 11 | } 12 | 13 | test { 14 | useJUnitPlatform() 15 | testLogging { 16 | events "passed", "skipped", "failed" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/example/project/Hello.java: -------------------------------------------------------------------------------- 1 | package com.example.project; 2 | 3 | public class Hello { 4 | 5 | public static void main(final String[] args) { 6 | System.out.println("Not the right string, tests will fail!"); 7 | } 8 | 9 | } 10 | 11 | -------------------------------------------------------------------------------- /src/test/java/com/example/project/TestHello.java: -------------------------------------------------------------------------------- 1 | package com.example.project; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.DisplayName; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.CsvSource; 9 | 10 | import java.io.*; 11 | 12 | public class TestHello { 13 | 14 | @Test 15 | public void testHelloWorld() 16 | { 17 | PrintStream originalOut = System.out; 18 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 19 | System.setOut(new PrintStream(bos)); 20 | 21 | // action 22 | Hello.main(null); 23 | 24 | // assertion 25 | assertEquals("Hello world!\n", bos.toString()); 26 | 27 | // undo the binding in System 28 | System.setOut(originalOut); 29 | } 30 | } 31 | --------------------------------------------------------------------------------