JetBrains/educational-plugin

Run task with argument

alfianyusufabdullah opened this issue · 6 comments

How to run/check task with value from parameter in main function? it's posible?

Undin commented

@alfianyusufabdullah Hi!
Do you mean check how main function works if some particular command line argument is passed into it? or something else?

Undin commented

@alfianyusufabdullah
For example, you can write something like this

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class Tests {
    
    @Test
    public void test() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStream));
        YourClassWithMain.main(new String[]{"YourCommandLineArg"});
        String output = outputStream.toString();
        // check output here
    }
}

in kotlin i can't direct access the main function and the command line argument will be set from Run/Debug Configuration. it's posible to use that? @Undin

Undin commented

in kotlin i can't direct access the main function

I can come up with only one case when it's possible - main function is located in private class. But in this case you can make it public. Could you provide a precise example what you want to do?

the command line argument will be set from Run/Debug Configuration. it's posible to use that?

But they won't be taken into account while task checking because task check doesn't use existing run configuration. So no, you can't use command line arguments from run configuration for task checking

thanks for the time @Undin , i think my case is close.