1

I am learning unit testing and am getting confused by what a test is and the boundries of where to go with them.

So the answer in this question is very clear - https://stackoverflow.com/a/1257583/445330

The point that stands out to me is the likes of "It talks to the database".

In my particular system i am learning with, i need to test for particular nodes in a xml tree - represented as an object.

Aside from loading this object, how can I test for the presence of this value - or am i confusing unit testing and functional testing?

Community
  • 1
  • 1
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • The *"it talks to the database"* is (together with 2 other options from list) concrete example of utilizing external resources in unit test (which you shouldn't really do due to their non-determinism). XML, on the other hand, can be easily prepared as "local" resource (see [akuhn's answer](http://stackoverflow.com/a/14164720/343266)). – k.m Jan 04 '13 at 21:07

3 Answers3

2

The gist of the answer that you’re linking to is that

unit tests shall not depend on external resources.

so as long as your XML tree is programmatically created and stored in memory only rather than loaded from the filesystem (or any other external resource) you’re doing it right.

Programmatically creating XML trees can be awkward, so what you can do is to store the tree as an XML file in the same package as your tests and then load them with

this.getClass().getClassLoader().getResourcesAsStream(name);

which does not count as an external resource because it is loaded from the classpath of your test suite.

akuhn
  • 27,477
  • 2
  • 76
  • 91
  • The xml tree/object is generated from a merged mixture of values in configuration files and values from a database table. These values are merged into 1 large configuration object which is essentially an xml tree. I am trying to test for the presence of values/nodes in this object. – Marty Wallace Jan 04 '13 at 21:15
  • That sounds quite messy. Often being able to test an application free from external dependencies requires changing the application such that all external dependencies are captured by a separate layer, which can the be stubbed away for testing. You best look into stubs and mocks. – akuhn Jan 04 '13 at 21:29
  • I thought about mocks. If i use a mock and fake a value, doesnt that defeat the purpose of checking for a desired value of the object property? – Marty Wallace Jan 04 '13 at 22:15
  • Depends. Sounds quite useful to test that a given *mixture* of configuration inputs leads to the correct *final* value. Less so if input and output are trivially the same. Essentially the purpose of tests is turn work you'd otherwise do manually into repeatable automated work, and we avoid external resources because we want our tests to be self-contained, so do whatever makes sense to achieve these two goals. – akuhn Jan 05 '13 at 03:13
  • I will not say that it's a bad idea to store XML files in your solution for testing purposes, but it's not exactly the most "maintain friendly" way. And, to add to that, it can be very tricky to test classes that rely on XML structures since they are hard to mock. XML and interfaces aren't going well together since inheritance is a concept which doesn't exist in XML. So, what I would strive for, is encapsulating the XML magic in a wrapper which does the reading/writing, and write your logic in the actual class that is responsible for adding function, and test that. – bas Jan 05 '13 at 20:21
0

If this is just a unit test, you are testing the code that is reading the xml tree and checking the tree for whatever you are expecting. You could load a test object in a number ways. One would be to have the unit test construct the object as a part of the setup for the test. Another would be to store the test version of the xml tree in a flat file (alongside the test classes) and have the test parse the file at test time.

The point is that the input to your test needs to be a control, not something that depends on the state of some external resource (like a database).

kldavis4
  • 2,177
  • 1
  • 22
  • 33
0

In my test I made not only one but more and more tests on a particula function, so Unit Test is a part of the possibilities. I select specific case for example in a division i use 0 for both operand, single operand etc, limit of the specific numerical interval, but also normal situation. All test are predictable, so when i change the code the execution of case assure the stability of change made.

In your case i think you are doing a single unit testing.

I accept the wikipedia definition of unit testing and functional testing.

Functional testing is wide and is program oriented. Unit testing is oriented to the minimal part of testable code and should be as much generic as possible.

In your specific case "In my particular system i am learning with, i need to test for particular nodes in a xml tree - represented as an object" if the result of function is the object you should have a well known input string, file , object static defined, and an assertion that the output for specific input is an object that has the particular node, or better extract the return object, extract the particular node and assert that the particular node should be equal to a particular value /object

I hope to be useful

user1594895
  • 587
  • 1
  • 10
  • 31