JUnit test
-
I just found this while trying to fix some JUnit tests that are causing our build to fail. It seems like an expensive way to do: int n = concreteClass.getSomeField();
public abstract class Abstract {
private int someField;
public int getSomeField() { return someField; }
// unrelated abstract stuff
}
public class Concrete extends Abstract {
// implement abstract stuff here
}
class TestConcrete extends TestCase {
public void testXXX() {
Concrete concreteClass = new Concrete();
try {
Field f = Abstract.class.getDeclaredField("someField");
f.setAccessible(true);
int n = f.getInt(concreteClass);
} catch (Exception e) {
fail(e.getMessage());
}
}
}
-
If you just call the function, you can't be sure that the field was actually set correctly. Duuuuuuuh.
-
@snoofle said:
I just found this while trying to fix some JUnit tests that are causing our build to fail. It seems like an expensive way to do: int n = concreteClass.getSomeField();
public abstract class Abstract {
private int someField;
public int getSomeField() { return someField; }
// unrelated abstract stuff
}
public class Concrete extends Abstract {
// implement abstract stuff here
}
class TestConcrete extends TestCase {
public void testXXX() {
Concrete concreteClass = new Concrete();
try {
Field f = Abstract.class.getDeclaredField("someField");
f.setAccessible(true);
int n = f.getInt(concreteClass);
} catch (Exception e) {
fail(e.getMessage());
}
}
}Accessing a field directly is always faster than calling a method to retrieve that field's value... except when accessing said field via reflection. Also, what's with declaring a ConcreteClass, surely the author should've declared avariable of type AbstractClass and assigned an instance of ConcreteClass to it?
-
@The_Assimilator said:
Accessing a field directly is always faster than calling a method to retrieve that field's value
Except when it doesn't. Like using a simple C# property.
-
@Sutherlands said:
Except when it doesn't. Like using a simple C# property.
Yeah, using even the simplest of C# properties in Java has HUGE performance implications.
-
@Chewbacca said:
@Sutherlands said:
You're bad with examples, aren't you? Java will also inline simple getters/setters.Except when it doesn't. Like using a simple C# property.
Yeah, using even the simplest of C# properties in Java has HUGE performance implications.
-
@Sutherlands said:
@Chewbacca said:
@Sutherlands said:
Except when it doesn't. Like using a simple C# property.
Yeah, using even the simplest of C# properties in Java has HUGE performance implications.
You're bad with examples, aren't you? Java will also inline simple getters/setters.
He's still right.