Home > Enterprise >  Spring Boot Cucumber test is exiting due to System.exit
Spring Boot Cucumber test is exiting due to System.exit

Time:10-03

I wrote a Cucumber test using Spring Boot to test another maven module project XX functions. Project XX has many micro main functions which is been run as a jar and been used by a different team. The test case is running fine but the project is each of those micro functions are using System.exit(statusCode) at the end of successful execution which apparently stops my test case

Like to know if there any way I can prevent my spring boot gets exited from System.exit(statusCode). I am using Spring boot 2.5.4

Can someone please help me on this.

Update 1

As per the suggestion from Gaël J I used System Rules and tried like as shown below in my cucumber Step Definition but still it did'nt work

import org.junit.Rule;
import org.junit.contrib.java.lang.system.Assertion;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;

public class StepDefs{

    @Rule
    public final ExpectedSystemExit exit = ExpectedSystemExit.none();
    :
    :
    :
    @When("^the function is executed$")
    public void functtionIsExecuted() {

    exit.expectSystemExitWithStatus(0);
        exit.checkAssertionAfterwards(new Assertion() {
            @Override
            public void checkAssertion() throws Exception {
                System.out.println("This is executed AFTER System.exit() method!");
            }
        });
        System.out.println("This is executed right before System.exit().");
        String args = {"some args"}
        new MailExecutor().main(args);
    }
} 

CodePudding user response:

It's usually wise not tot depend too much on libraries. In this case you could write an extension on the MailExecutor class and override the function that calls System.exit() with an implementation that doesn't call it. You can then test the MailExecutor class through the extension class. This technique is described in "Working effectively with legacy code" by Michael Feathers.

CodePudding user response:

This is more of a Software Engineering problem. So the solution really depends.

First I'd try to get the original code changed and released. This may not be feasible if it no longer exists or if the owners are uncooperative.

Failing that I'd consider forking the code and making my own release. This may not be feasible if it no longer exists or you can't bear the maintaince burden.

Failing that I'd consider testing the application out of process. Rather the using unit tests, start the application in a new process and observe the input/output.There are some tools to test CLI applications.

Then going forward I'd consider the need for these applications, the exact setup of this relationship between teams and everything else. The problem really isn't that these tools call System.exit. It's everything around it that lead up to them doing so that is.

  • Related