+1 vote
in JUnit by
To create a JUnit test case for the existing class FirstDayAtSchool.java, right-click on it in the Package Explorer view and select New -→ JUnit Test Case. Change the source folder so that the class will be located to test source folder and ensure that the flag New JUnit4 test is selected.

Below, there is the code of the class named FirstDayAtSchoolTest.java, which is our test class: FirstDayAtSchool.java JUnit Tutorial 15 / 26 package com.javacodegeeks.junit;

import static org.junit.Assert.*;

import org.junit.Test;

 public class FirstDayAtSchoolTest

{

 FirstDayAtSchool school = new FirstDayAtSchool(); String[] bag1 = { "Books", "Notebooks", "Pens"

};

 String[] bag2 = { "Books", "Notebooks", "Pens", "Pencils" };

@Test public void testPrepareMyBag()

{

 System.out.println("Inside testPrepareMyBag()"); assertArrayEquals(bag1, school.prepareMyBag());

}

 @Test public void testAddPencils()

 {

 System.out.println("Inside testAddPencils()"); assertArrayEquals(bag2, school.addPencils());

}

 }

Now we can run the test case by right-clicking on the test class and select Run As → JUnit Test. The program output will look like that:

Inside testPrepareMyBag() My school bag contains: [Books, Notebooks, Pens] Inside testAddPencils() Now my school bag contains: [Books, Notebooks, Pens, Pencils] and in the JUnit view will be no failures or erros. If we change one of the arrays, so that it contains more than the expected elements: String[] bag2 = { "Books", "Notebooks", "Pens", "Pencils", "Rulers"}; and we run again the test class, the JUnit view will contain a failure:

Else, if we change again one of the arrays, so that it contains a different element than the expected:

String[] bag1 = { "Books", "Notebooks", "Rulers" };

and we run again the test class, the JUnit view will contain once again a failure:

Related questions

+1 vote
asked May 18, 2020 in JUnit by GeorgeBell
+1 vote
asked May 19, 2020 in JUnit by GeorgeBell
...