Forum Discussion

stephensurya's avatar
stephensurya
New Contributor
15 years ago

REST testing - how to pass path parameters from Java code

(I'm a newbie so forgive me if the question is pretty basic)

Hi,
I'm trying the Junit integraiton with the example at:
http://www.soapui.org/userguide/command ... unner.html

I see two ways to execute a particular test case from Junit - the examples are "testRunner" and "testTestCaseRunner". I got both of them working in my end to test my REST services.

Now, how do I pass a PATH parameter from Junit - say my REST path looks like this:

http://localhost:8080/myapp/rest/users/{userId}/purchase

I want to set the value of {userId} from my Junit test case - how do I do this?

thanks,
Stephen

2 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    One way to do this is to use a TestCase property:

    Define a new Test Property in the TestCase called (for example) "userId".
    Set the userId parameter in the Request to ${#TestCase#userId} (which is  a property expansion for the newly created property).

    Now when you run the TestCase, first set the property value to whatever you want:



    WsdlProject project = new WsdlProject( "src/dist/sample-soapui-project.xml" );
    TestSuite testSuite = project.getTestSuiteByName( "Test Suite" );
    TestCase testCase = testSuite.getTestCaseByName( "Test Conversions" );

    testCase.setPropertyValue("userId", "whatever");

    // create empty properties and run synchronously
    TestRunner runner = testCase.run( new PropertiesMap(), false );
    assertEquals( Status.FINISHED, runner.getStatus() );



    Regards,
    Dain
    eviware.com
  • Dan,
    Thanks for the help. Coincidentally, I stepped on those same methods myself. But for my requirement I wanted something global so I ended up doing this:

    I created a properties file "test.properties" with few properties. All my Resources in the SoapUI test cases use the ${#Global#propertyName}.

    Now, for running the test cases from the tool I load the properties file from "File ==> Preferences ==> Global Properties"

    For running the same test cases from Junit I do:

    SoapUITestCaseRunner runner = new SoapUITestCaseRunner();

    String properties[] = new String[5];
    properties[0] = "propertyName0=" + propertyValue0;
    properties[1] = "propertyName1=" + propertyValue1;
    properties[2] = "propertyName2=" + propertyValue2;
    properties[3] = "propertyName3=" + propertyValue3;
    properties[4] = "propertyName4=" + propertyValue4;

    runner.setGlobalProperties(prop);
    runner.setTestCase(testCaseName);
    runner.run();

    This is working for now.

    thanks,
    Stephen