I have to write the following unit test cases in testng:
saveProductTest which would return productId if product details are saved successfully in DB.
>
With all due respect to simendsjo, the fact that all tests should be independent from each other is a dogmatic approach that has a lot of exceptions.
Back to the original question: 1) use dependent methods and 2) store the intermediate result in a field (TestNG doesn't recreate your instances from scratch, so that field will retain its value).
For example
private int mResult;
@Test
public void f1() {
mResult = ...
}
@Test(dependsOnMethods = "f1")
public void f2() {
// use mResult
}