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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:10:26+00:00 2026-06-16T18:10:26+00:00

I need to test this method – compare() . Can You get advice? How

  • 0

I need to test this method – compare(). Can You get advice? How better I can do this(all part if, else-if, else).

public class AbsFigure {

class AreaCompare implements Comparator<FigureGeneral> {

    @Override
    public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
        double firstValue = oneFigure.area();
        double secondValue = twoFigure.area();
        int result = 0;

        if (firstValue > secondValue)
            result = 1;
        else if (firstValue < secondValue)
            result = -1;
        else
            result = 0;

        return result;
    }
}

After this recomendations – we have next picture (Thank YOU guys a lot!):

public AreaCompare areaCompare = new AreaCompare();

@Test
public void testEqual() {
    FigureGeneral oneFigure = new Rectangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(2.0, 2.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be equal", result == 0);
}

@Test
public void testGreaterThan() {
    FigureGeneral oneFigure = new Triangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(1.0, 1.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be greater than", result >= 1);
}

@Test
public void testLessThan() {
    FigureGeneral oneFigure = new Rectangle(1.0, 1.0, "rectangle");
    FigureGeneral twoFigure = new Triangle(2.0, 2.0, "triangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be less than", result <= -1);

All is normal testing now.

  • 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-06-16T18:10:27+00:00Added an answer on June 16, 2026 at 6:10 pm

    Just instantiate your comparator class and pass in objects:

    public class Test extends TestCase {
        class AreaCompare implements Comparator<FigureGeneral> {
    
            @Override
            public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
                double firstValue = oneFigure.area();
                double secondValue = twoFigure.area();
                int result = 0;
    
                if (firstValue > secondValue) {
                    result = 1;
                } else if (firstValue < secondValue) {
                    result = -1;
                } else {
                    result = 0;
                }
    
                return result;
            }
        }
    
        private final AreaCompare areaCompare = new AreaCompare();
    
        @Test
        public void testEqual() {
            FigureGeneral oneFigure = new FigureGeneral();
            FigureGeneral twoFigure = new FigureGeneral();
            int result = areaCompare.compare(oneFigure, twoFigure);
            assertTrue("expected to be equal", result == 0);
        }
    
        @Test
        public void testGreaterThan() {
            FigureGeneral oneFigure = new FigureGeneral();
            FigureGeneral twoFigure = new FigureGeneral();
            int result = areaCompare.compare(oneFigure, twoFigure);
            assertTrue("expected to be greater than", result >= 1);
        }
    
        @Test
        public void testLessThan() {
            FigureGeneral oneFigure = new FigureGeneral();
            FigureGeneral twoFigure = new FigureGeneral();
            int result = areaCompare.compare(oneFigure, twoFigure);
            assertTrue("expected to be less than", result <= -1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to test a method belonging to a service class. This service class
Why does this method (test) need a return value (it is always true)? public
I have this method to unit test: public virtual TEntity Get(int id) { return
I have this JUnit test that I need help developing a Interface and Class
I'm trying to write a test for this class its called Receiver : public
I'm beginner, and keep yourself in hands. I need to test this method -
I have this method as part of a larger class. I'm trying to write
I have this method to test : public static Date getDateSinceUTC(CstOrderBean orderBean) { int
I have this method: [WebMethod] public OpenAccountResult OpenAccount() { OpenAccountResult test = new OpenAccountResult(/*true*/)//the
I need to put test values in a class, somehow like in this simplified

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.