John B.
John B.

Reputation: 13

mvn test fails, but tests pass if run per package

I have a project with the following structure

src
  |_ main
  |   |_ java
  |       |_ com.company.product
  |           |_ packageA
  |           |_ packageB
  |_ test
      |_ java
          |_ com.company.product
              |_ packageA
              |_ packageB

When running mvn test, my tests in packageA pass and the tests in packageB fail. When running mvn test -Dtest="com.company.product.packageB.**", the tests in packageB pass. Furthermore, running mvn test -Dtest="com.company.product.**" also fails the packageB tests but not the packageA tests. Why does mvn test not pass all tests that should pass?

Details on tests in packageB:

@Test
void createNew() {
    String user = "testUser";

    //This calls a third party API that is throwing a 
    //InvocationTargetException when running packages together
    Connection connect = new Connection(user);
    String resultText = connect.getResultText();

    assertNotNUll(connect);
    assert (resultText).equals("Process Complete");
}

The jar required for running the third party API call is included in the pom as follows.

    <dependency>
        <groupId>com.third.party.api</groupId>
        <artifactId>third-party-api</artifactId>
        <version>1.0.0</version>
    </dependency>

Using Java 1.8.0_74 and Maven 3.5.4.

EDIT: Error returned by Maven:

createNew()  Time elapsed: 0.001 sec  <<< ERROR!
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.company.product.packageB.MyTest.createNew(MyTest.java:11)
Caused by: java.lang.reflect.InvocationTargetException
        at com.company.product.packageB.MyTest.createNew(MyTest.java:11)
Caused by: java.lang.RuntimeException: Error when creating RpcClientStub. Cause : java.lang.NoClassDefFoundError: Could not i
nitialize class com.third.party.apitransport.session.ArRpcCallContext
        at com.company.product.packageB.MyTest.createNew(MyTest.java:11)

...

Results :

Tests in error:
  MyTest.createNew:11 » Runtime java.lang.reflect.InvocationTargetEx...
  MyTest.createAndUpdate:29 » Runtime java.lang.reflect.Invocation...
  MyTest.connect:51 » Runtime java.lang.reflect.InvocationTarget...

Tests run: 9, Failures: 0, Errors: 3, Skipped: 0

EDIT: The fix was to add cleanup as Ivan pointed out in the comments.

private static String systemOsName;
@BeforeAll
public static void setOsName(){
    systemOsName = System.getProperty("os.name");
}
...
@AfterAll
public static void cleanup(){
    Constants.setFilePathSeparator("");
    System.setProperty("os.name",systemOsName);
}

Upvotes: 1

Views: 278

Answers (1)

Chad Jensen
Chad Jensen

Reputation: 60

If there are multiple tests that are setting the System.setProperty("os.name", "windows") value, then this will need to be reset at the end with a cleanup if that value is used to determine a value later in your package b tests.

Upvotes: 1

Related Questions