vendredi 8 mai 2015

Instance variable in mock class

We're trying to mock out our random distributions but after making our mocks a bit more flexible we've been having trouble. It seems instance variables are not kept.

This is our mock:

public static final class MockBern extends MockUp<FBernoulliDistribution> {

  private double m = 0.1;

  @Mock
  public void $init(double m) {
     System.out.println("Created bern, m=" + m);
     this.m = m;
  }

  @Mock
  public ThreadLocal<Boolean> sample() {
     return new ThreadLocal<Boolean>() {
        @Override
        protected Boolean initialValue() {
           System.out.println("m=" + m);
           return m > 0.5;
        }
     };
  }
}

When the mock is created m is set to 1.0, and there's only a single call to the constructor, but when the sample() is called it's 0.1 again. Why?

Aucun commentaire:

Enregistrer un commentaire