Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33489
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Instantiation HELP! |
| Date | 2016-01-20 12:48 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <n7oh4l$p99$1@dont-email.me> (permalink) |
| References | <7ff59f13-978b-43ad-9f98-12fb73a1b1c9@googlegroups.com> |
On 1/20/2016 10:45 AM, knoll2roll@gmail.com wrote:
> Hi guys,
>
> I am fairly new to Java coding and am currently working on writing a test.
> I am using OO practices and have some questions.
>
> I am wondering if there is a smarter way using Java to instantiate the object classes into my test class.
>
> please see the example below:
>
> public class DummyTest {
>
> private ObjectClass objClass;
>
> public DummyTest() {
> objClass = new ObjectClass();
> }
>
> Once i do the following i can then use the objects within the 'ObjectClass' class within my tests.
>
> This seems to be a two step process when ever i use another object class. So i was wondering if there was a smarter, shorter way of doing the above, while achieving the same results?
I'm not sure what you find clumsy about this: Your tests need to
operate on an instance of ObjectClass, so you need to create that
instance. There's a shorthand you can use:
public class DummyTest {
private ObjectClass objClass = new ObjectClass();
// no explicit constructor
// ... tests using `objClass' ...
}
(and you might want to put `final' on the objClass declaration, too).
With this shorthand, the construction of objClass gets inserted into
all the DummyTest constructors, including the one Java creates for you
if you don't write one yourself. Thus, it winds up being (pretty much)
equivalent to what you already have, but with less source code.
But if you're testing a class that has more than one constructor,
or has a constructor that takes parameters, this pattern isn't so
helpful. Also, if you're testing a class whose methods should behave
differently depending on how their instances were constructed, this
won't do. You'll need something more like
public class DummyTest {
public void testDefaultConstructor() {
ObjectClass instance = new ObjectClass();
// ... tests
}
public void testIntegerConstructor() {
int[] valid = new int[] { 1, 42, 287 };
for (int param : valid) {
ObjectClass instance = new ObjectClass(param);
// ... tests
}
int[] bogus = new int[] { 0, -1, 1001 };
for (int param : bogus) {
try {
ObjectClass instance = new ObjectClass(param);
fail("didn't throw; got " + instance);
} catch (IllegalArgumentException ex) {
// Thanks: I needed that.
}
}
// ...
}
Unless the construction of the tested object is expensive -- maybe
it needs to consult a database located in Burkina Faso or something --
I think you're better off to let each feature test use its own instance
than to have all the tests share one common instance. If nothing else,
doing so allows tests to run in parallel rather than one at a time,
which could be useful if you eventually have a lot of tests.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Instantiation HELP! knoll2roll@gmail.com - 2016-01-20 07:45 -0800 Re: Instantiation HELP! Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-01-20 12:48 -0500
csiph-web