What sources of data/parameters should I use when Unit testing?

I am performing unit tests across a large system, made up of hundreds of different .java files, all forming the back-end.

In my unit tests, should I pass the variables as they are from the other methods (calling the methods within my test method) or do I test using values that are created/declared in the test method?

I'm thinking that I should come up with values specifically for the test method since these are unit tests and I am trying to test each method in isolation, but I'm not certain about this. Do I need to copy the functions exactly that fetch said data into the unit tests?


Generally speaking, you should create the values in the test itself, thus better isolating the system under test. There are other kinds of tests, as @James mentions, and you can use junit for integration tests as well as unit tests. But at the heart of junit testing is unit testing, and proper unit tests isolate the system under test.

Update in response to your comment:

So for example if you have classes A and B:

public class A {
  private B b = new B();

  public void callB() {

public class B {
  public void process(int n) {
    // do stuff with n

Your test class for B could simply instantiate its own B and call b.process(17) and b.process(22) without any reference to A, nor need to subclass A. In order to test A in isolation from B you would need some kind of mocking mechanism which would replace B with a mock. Such a test would only use the mock to verify that b.process had been called with 17 and 22, not worrying about what happened as a consequence of those calls.

The answer is, again, it depends. :)

It depends on what you are trying to test. If you are trying to unit-test a function whose arguments might be returned from other functions or external inputs, then in my opinion, you should manufacture the test arguments in your tests, so that you can test all kinds of scenarios and corner cases.

If you are trying to test the INTEGRATION between a function and other functions or inputs, you are sort of doing an integration test. Then, you'll have to wire the different functions together and execute your test cases with upstream inputs.

Need Your Help

How to Get Current (US) State from iPhone Core Location?

iphone xcode geolocation core-location

I'd like my iOS app to display information specific to the user's current state (i.e. California, Oregon, etc). How is this done using core location? The CLLocation class has coordinates—how do I t...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.