First time writing junit: Need suggestions

Viewed 60

I wrote a small code to sort one of the fields called "Type", and now I have to write Junit test for it.I am totally new writing test,so I would like to know how many conditions do i consider for it or not. for example I wrote test code for: -

  1. testCompareBothNull,
  2. testComparePojo1NullPoj02NotNull,
  3. testComparePojo1NotNullPoj02Null,
  4. testComparePojo1NotNullPoj02NotNull

.

so I would need your suggestions.I thank you in advance.

3 Answers

As a rule of thumb, this about tests like a tool that is here to help you the programmer to make sure that the code you wrote behaves as expected.

With this in mind, what are the inputs you would like to test so that you could be sure that your code works?

You say:

I wrote a small code to sort one of the fields called "Type"

So this code must be in some method, right?

Assuming you have something like this: public List<MyObject> sort(List<MyObject> input) you can verify on the following cases:

  1. Pass null, what is the result, is it expected?
  2. Pass empty list, what is the resulst, is it expected?
  3. Pass List with one element - will it sort?
  4. Pass List with Element(s) having MyObject#type field null. Is the result expected?
  5. Pass List which is already sorted/
  6. Pass List that is not sorted, make sure now its sorted.
  7. Pass List with identical objects (sort(listOf(a, a, a, a))
  8. Pass List with null-s:(sort(null, new MyObject(..), null)

All-in-all its up to you do decide how far you can go in order to feel that the code works.

Since you are new to Unit test framework I suggest you to look at Data Driven Testing i.e. DDT.

Data Driven testing generally means executing a set of steps with multiple sets of data. Here you create your Data using DataProvider and pass them as an argument to test methods , thus eliminating writing the redundant line of code for test cases.

TestNG is one popular framework for the same.

So in your scenarios u just write one test case

@Test(dataProvider = "type-null-check-Data")
public void test(Foo foo1, Foo foo2){
   //Invocation of actual code with foo1 & foo2
   //The scenarios can be
   // 1. foo1 is null
   // 2. foo2 is null
   // 3. foo1.type is null
   // 4. foo2.type is null
} 

Scenarios creation as below

@DataProvider(name="type-null-check-Data")
    public Object[][] dataProviderMethod() {
        Foo foo1 = new Foo();
        foo1.setType(null);

        Foo foo2 = new Foo();
        foo2.setType("anyValue);

        Foo foo3 = new Foo();
        foo3.setType("anyValue");

        return new Object[][] { 
            {null, null},
            {null, foo1},
            {foo1, null},
            {foo2, foo3},
        };
    }

Thank you everyone. You guys are great. Very useful comments. Now I kind of have an idea where to go from here. I never had to write unit test code before; just tested manually looking at pages or applications, so I was not sure what to cover or NOT, but yes I know now that writing code to test is also big part of the development.

Related