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

  • SEARCH
  • Home
  • 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 7668663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:25:04+00:00 2026-05-31T15:25:04+00:00

Please, I am new to Java. How can I write a JUnit test for

  • 0

Please, I am new to Java. How can I write a JUnit test for the program below:

Program to Test:

package codekeeper;

/**
 *
 * @author henryjoseph
 */
import java.util.*;
import java.io.*;

public class CodeKeeper {

    ArrayList<String> list;  //no specific amount..

    String[] codes = {"alpha","lambda","gamma","delta","zeta"};

    public CodeKeeper (String[] userCodes)
    {
        list = new ArrayList<String>();

        for(int i =0; i<codes.length;i++)
           addCode(codes[i]);

        for(int i =0; i<userCodes.length;i++)
            addCode(userCodes[i]);

        for(String code:list)
            System.out.println(code);
    }

    final void addCode(String code)
    {
        if(!list.contains(code))
            list.add(code);
    }

    public static void main(String[] args) {
        System.out.print("Enter your name and press Enter: ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String name = null;
        String[] argu = new String[] {name};
        try {
            name = br.readLine();

            argu = new String[] {name} ;
        }catch (IOException e) {
            System.out.println("Error!");
            System.exit(1);
        }
        CodeKeeper keeper = new CodeKeeper(argu);
    }
}

Sample Test Code:

public class MyClassTest {
    @Test
    public void testMultiply() {
        MyClass tester = new MyClass();
        assertEquals("Result", 50, tester.multiply(10, 5));
    }
}

Is this a standard way of writing the Junit Tests?

  • 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-31T15:25:06+00:00Added an answer on May 31, 2026 at 3:25 pm

    Some background first then some examples. When you write unit tests against your code you are testing a very specific and finite case of your application. What that means is that each unit test should cover at most one (1) scenario that a function (method) can receive. If you want to test multiple functions together you would be performing integration testing. Now if we take the following function that you have declared:

        final void addCode(String code)          
    {   
               if(!list.contains(code))           
                   list.add(code);           
    } 
    

    Assume that codeKeeper is properly initialized.
    An appropriate unit test would look like this:

    @Test(expected= NullPointerException.class)   
    public void testAddCode_1() throws Exception  
    {  
        codeKeeper.addCode(null);  
    }  
    

    Another appropriate usage is the following:

    @Test()  
    public void testAddCode_2() throws Exception  
    {  
           codeKeeper.addCode("myMagicCode");
           assertTrue(codeKeeper.getList().contains("myMagicCode");  
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java Code need to write an equivalent in objectiveC. CAn you please help me
I am new in Java so please be patient. It is common to map
I am new to boost - can you please tell me what are the
Please help me to write a trigger that adds new rows to a table.
Please forgive me if any of this is wrong -I'm completely new to Java.
can someone please tell me the elegant way of inserting a java date instance
I have a client/server program and applet. I will show the code below. Can
I am very new to Java. I am writing a program to read a
I'm trying to read and write the data to the server using java program.
I am new to webservices in general. I am trying to write a 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.