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 6814257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:40:04+00:00 2026-05-26T20:40:04+00:00

I am testing Java app with JUnit. The following is the source code of

  • 0

I am testing Java app with JUnit. The following is the source code of a specific method:

public class Surgery {
    Vector<Patient> patients;
    String name;

    public Surgery(String name) {
        patients = new Vector<Patient>();
        this.name = name;
    } 

     public Patient findPatient(String name) {
         Iterator<Patient> patientIt = patients.iterator();
         while(patientIt.hasNext()) {
             Patient next = patientIt.next();
             if (next.getName().equals(name))
                return next;
         }
         return null;
     }  

This is JUnit test method:

public class SurgeryTest {
    private Vector<Patient> vector;
    Surgery surgery_N =new Surgery("Teddy");
    ByteArrayOutputStream ans = new ByteArrayOutputStream();
    final String separator = System.getProperty("line.separator");

    @Test
    public void testFindPatient() {
        surgery_N.findPatient("Teddy");
    }
}

I need to test each statement in the source code method. I stuck, don’t know what else to do. Any solution?

  • 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-26T20:40:05+00:00Added an answer on May 26, 2026 at 8:40 pm

    Your Surgery class contains no way to add patients to it from the code sample you have given us, so your unit test should be finding nothing.

    To test each statement in the source code method you should create multiple tests that cover each one of the possible paths in your code. That means, in your tests you will want to test for the scenario where you return a patient name if it exists, and one for where the patient doesn’t exist (returning null).

    Here’s some example methods for you to work from:

    public void testFindPatientWherePatientExists() {
        Patient thePatient = surgery.findPatient("Teddy");
        assertEquals("Teddy", thePatient.getName());
    }
    
    public void testFindPatientWherePatientDoesntExist() {
        assertNull(surgery.findPatient("I dont exist"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're running a java/hibernate app going against ORACLE 10g in TESTING. Once in a
I was testing Thrift on java, but this code give me some errors.. ClientVoiceToTextHandler
All, I'm trying to do some unit testing in some archaic java code (no
I am looking to obfuscate our Java web app code within our existing Ant
On PhoneGap on Android you can modify the App.java class to load any url.
I'm testing some functions with Android, JNI and NDK. I have the following JAVA
package info.testing; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import android.app.Activity; import android.os.Bundle;
I am using log.properties to configure logging for a Java app. Each class gets
I've created Google App Engine project in Java using Eclipse following the book Beginning
New -> Android project for testing the problem Code for Test.java : package test.density.yeah;

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.