简体   繁体   中英

Add a main method to some classes for simple testing

I have some classes throughout a maven project. I would like to add a main method to some of those classes for basic testing while developing.

I tried declaring the class to run with:

mvn exec:java -Dexec.mainClass="huru.util.Async"

but that command looked in my pom.xml file and it ran some pre-configured setup and started my server up as usual. How can I run a specific file (not my regular main class), but still load up the necessary dependencies?

note that for testing I need most of the dependencies in pom.xml, so I will probably need mvn to run the class that I need to test, I can't run it directly with javac.

update sadly, I may need to create a profile in pom.xml since maven can't seem to do very much from the command line. I don't know much about profiles and since I have none in my pom.xml file right now, I am a bit scared of adding that section.

As suggested in the comments, one solution is to skip putting a main method in the class I want to test, but instead create a junit test...this works at the command line:

 mvn -Dtest=AsyncTest test

where my test looks like:

package huru;

import huru.util.Async;
import io.vertx.core.Vertx;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;

@RunWith(VertxUnitRunner.class)
public class AsyncTest {


  @Test
  public void test(TestContext tc) {
    Async.Parallel(Arrays.asList(

      v -> {
        v.done(null, null);
      }

    ), (e, results) -> {

    });
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM