4

Programming books usually preach to only test a single method in a unit test. This is supposed to keep the tests independent of each other and reduce the amount of work needed to rewrite the tests if a particular part of the system changes.

But what about calls to set up the object under test?

@Test
public void testMethod_someState_someExpectation() {
    MyObject obj = new MyObject();
    obj.setSomeStuff();
    obj.doSomeStuffToGetIntoTheSpecifiedState();
    Test.assertEqual(obj.testMethod(), someValue);
}

Most of my unit tests look like this, because my object under test might be in many different states when calling my testMethod() and i would like to test how it behaves. And i guess most of the object oriented code out there doesn't consist of pure functions.

Are these kinds of tests automatically considered Integration testing, or is this kind of setup normal for unit testing?

9

First, there is no strict, widely accepted definition of when a test is not a unit test any more - the boundaries are blurry and to some degree opiniated.

However, my understanding of the word "integration tests" involves that there must be at least two "units" or "components" of code, and some production code which integrates them. An integration test then is a piece of code which checks that integrated scenario of the production code, not some "integrational setup" in test code.

  • That is how i interpreted the term integration test aswell. Thank you for clearing up my confusion! – Luca Fülbier Jul 4 '16 at 21:07
4

There is no clear definition of what is a "unit test". The definition I find most useful though is the that if an isolated unit of code (which might be a method, a class, or way more than that) can be tested without any external side effects, then its a unit test. The "litmus test" being, can I run many of these tests in parallel, without them affecting each other? If so, they are unit tests.

If the code under test has side effects (eg, reads/writes to files, DB tables, or - and this hopefully never occurs for you - global state), then they have to be run one at a time and so are integration tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.