2

Here is the scenario: I generate Random Data in one class and use it as an argument in a method in the same class. How can i use the exact same value in another class ?

Here is a simplified version of my classes:

public class A {
    @Test
    public void aMethod() {     
        RandomStringUUID ruuid = new RandomStringUUID();
        }
}

Then:

public class B { 
        @Test
    public void bMethod() {
        // Want to use ruuid , without instantiating RandomStringUUID 
                // as i dont want new values, i want the one from class A
        }
}
Louth
  • 11,401
  • 5
  • 28
  • 37
kamal
  • 9,637
  • 30
  • 101
  • 168
  • 1
    What's the relationship between class A and some class B? – user1201210 Sep 26 '12 at 13:10
  • 1
    Tests should be reproduceable. You really shouldn't use random or other unpredicatable test data in your tests (data from production databases, dynamic ip addresses, etc). Imagine a test fails and you'll never know why just because each test run has different parameters.. – Andreas Dolk Sep 26 '12 at 13:16
  • @Andreas_D In my case, its important to use random data, since this is part of a Test Framework, and since many people are using this against the SAME web service, hence uniqueness is a MUST – kamal Sep 26 '12 at 13:24
  • @Dynguss I am not sure what you mean by relationship, as you can see that Class A and Class B are not extending one form another, or implementing any interface, there are simple classes, but sharing Data between them, Consider Class A as a Utility Class for Class B – kamal Sep 26 '12 at 13:27
  • @kamal "Consider Class A as a Utility Class for Class B" Thanks, that's what I was looking for. – user1201210 Sep 26 '12 at 13:30

4 Answers4

4

use a static approach:

public class A {

     private final static RandomStringUUID ruuid = new RandomStringUUID();

     public final static RandomStringUUID getRuuid() {
         return ruuid;
     }

     @Test
     public void aMethod() {     
         RandomStringUUID ruuid = getRuuid();
     }
}

and

public class B { 
    @Test
    public void bMethod() {
         RandomStringUUID ruuid = A.getRuuid();
    }
}
sics
  • 1,298
  • 1
  • 11
  • 24
2

Make your variable a public static variable in class A

public static RandomStringUUID RUUID = new RandomStringUUID();

After the beatdown I'm getting in the voting count

private RandomStringUUID uuid;

private createUuid() {
    uuid = new RandomStringUUID();  //Or any other way you create it
}

public static RandomStringUUID getRuuidForOtherClassesToCheck() {
    return ruuid;
}

Not enough info on the OP's part to infer it's design rules or just a simple JAVA question on how to get a Variable from Class to Class. :D

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • How are you certain that the functional requirement and technical design (which the OP didn't tell anything about) allows it being a class variable? Note that it's originally been created in an instance method. Making it a class variable may have unwanted side effects. – BalusC Sep 26 '12 at 13:12
  • I hope you missed 'final' for this declaration. If not , then exchanging data via static variables is not something I would recommend to anyone. Unless you want to teach them bad programming habits. – Adam Dyga Sep 26 '12 at 13:13
  • 1
    @BalusC I don't and can't infer, looks to me like innocent JAVA straight up question – gtgaxiola Sep 26 '12 at 13:26
  • @gtgaxiola Thanks , dont see why i would get -1 rating for this question, when there is already interest and learning for all of us. Seems unfair on part of stackoverflow – kamal Sep 26 '12 at 13:32
0

Either have A give the value to B

public class B {  
    @Test 
    public void bMethod(RandomStringUUID ruuid) { 
    // Want to use ruuid , without instantiating RandomStringUUID  
            // as i dont want new values, i want the one from class A 
    } 
} 

Or have A make it available to be accessed by B, as @sics said

CPerkins
  • 8,968
  • 3
  • 34
  • 47
0

We sometimes test use helper classes. That could be a solution for your (test) case. So A wouldn't provide the value to B but both would obtain the same object from a third class:

public class TestHelper {

  private final static RandomStringUUID UUID = new RandomStringUUID();

  public static RandomStringUUID getDummyRandomStringUUID() {
     return UUID;
  }
}

public class A {
  @Test
  public void aMethod() {     
    RandomStringUUID ruuid = TestHelper.getDummyRandomStringUUID();
  }
}

(and the same with B)

It's still questionable for testing, because each test run uses a different UUID value, so the tests are not reproduceable. It would be better to define a static UUID that is used for every test run.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268