Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7044093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:22:06+00:00 2026-05-28T02:22:06+00:00

I am new to JUnit and facing difficulty in writing JUnit. I have an

  • 0

I am new to JUnit and facing difficulty in writing JUnit.

I have an interface and a class

Interface

public interface Student{

    List<String> getStudentList();
    void setStudentList(List<String> studentList);
    void createStudentList();
}

Class

public class StudentServiceImpl implements Student{

        private List<String> studentList

        public List<String> getStudentList() {
            return studentList;
        }


        public void setStudentList(final List<String> studentList){
            this.studentList = studentList;
        }

        public void createStudentList(){
            if (studentList == null) {
                studentList = new ArrayList<String>();
                studentList.add("John");
                studentList.add("Bill");
                studentList.add("Ricky");
                studentList.add("Jack");
            }
            setStudentList(studentList);
        }
    }

What is the best to write Junit for above class. What should be kept in mind while writing JUnit cases. Can somebody help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:22:07+00:00Added an answer on May 28, 2026 at 2:22 am

    Here is an example of IMHO well-written test case (written in notepad, please correct if does not compile):

    import static org.fest.assertions.Assertions.assertThat;
    
    public class StudentTest {
    
        private final Student student = new StudentServiceImpl();
    
        @Test
        public void shouldReturnEmptyListOfStudentsWhenNotInitialized() {
            //given
    
            //when
            List<String> students = student.getStudentList();
    
            //then
            assertThat(students).isEmpty();
        }
    
        @Test
        public void shouldReturnSomeStudentsWhenListCreated() {
            //given
            student.createStudentList();
    
            //when
            List<String> students = student.getStudentList();
    
            //then
            assertThat(students).containsExactly("John", "Bill", "Ricky", "Jack");
        }
    
        @Test
        public void shouldStorePreviouslySetListOfStudents() {
            //given
            student.setStudentList(Arrays.asList("Jane", "Bob"));
    
            //when
            List<String> students = student.getStudentList();
    
            //then
            assertThat(students).containsExactly("Jane", "Bob");
        }
    
    }
    

    Few tips:

    1. Keep tests short and simple

    2. Test name should describe what use case is being tested

    3. Separate setup code, tested logic and assertions

    4. Code should be easy to read. Use fluent and descriptive libraries like FEST

    Further improvements:

    1. The first test should fail, do you know why?

    2. There are still few tests missing, do you know which ones?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to junit testing and I have the following test - public
I have a simple JUnit test in Eclipse. public class HelloTest extends TestCase {
I have the following JUnit test: @Test public void testRunLocalhost() throws IOException, InterruptedException {
I have the following expectation in a JUnit test class: CustomEvent myCustomEvent = new
I am fairly new to Java. I have constructed a single JUnit test class
I have a JUnit test that is as follows: @Test public void testToDatabaseString() {
I am new to Java and junit. I have the following peice of code
Writing Junit Tests for my spring application. Because I am new at this I
Have jUnit test, which initializes my bean: ShowProducts sp = new ShowProducts(); got NullPointerException
I am new to Java testing with JUnit. I have to work with Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.