How to quickly check Java method in Eclipse?

I have a simple method in My Java code like

private int specialAdd (int a, int b) {
  if(a < 100) {
    return 100 * a + b;
  } else {
    return a + b;
  }
}

Is there a way to "run / debug selected code" with given parameter values ​​in eclipse? I would really like to run this method and view the result for a pair of aand b.

+4
source share
6 answers

Is there a way to "run / debug selected code" with given parameter values ​​in eclipse?

No, Eclipse needs a regular method mainor method @Testas an entry point.

When I want to perform a “one” mini-test, what I usually do is

class YourClass {
    private int specialAdd (int a, int b) {
       if(a < 100) {
           return 100 * a + b;
       } else {
           return a + b;
    }

    public static void main(String[] args) {
        System.out.println(new YourClass().specialAdd(10, 100));
    }
}

( ) . (Pro tip: main Ctrl + Space, )


JDK 9 jshell :

$ ./jshell
|  Welcome to JShell -- Version 1.9.0-internal
|  Type /help for help

-> int specialAdd(int a, int b) {
>>     if (a < 100) {
>>         return 100 * a + b;
>>     } else {
>>         return a + b;
>>     }
>> }
|  Added method specialAdd(int,int)

-> specialAdd(10, 5);
|  Expression value is: 1005
|    assigned to temporary variable $2 of type int
+6

, , JUnit. , .

jUnit, @Test. Asset yo / . :

   @Test
   public void testHelloWorld() 
   {
      h.setName("World");
      assertEquals(h.getName(),"World");
      assertEquals(h.getMessage(),"Hello World!");
   }

> JUnit:

enter image description here

JUnit Maven:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>
+1

, . , JUnit, .

public class YourClassTest {

    private YourClass toTest = new YourClass();

    @Test
    public void specialAddMultiplesSmallFirstAddendBy100() {
        int result = toTest.specialAdd(99, 7);
        assertEquals(9907, result);
    }

    @Test
    public void specialAddDoesNotIncreaseLargeFirstAddend() {
        int result = toTest.specialAdd(100, 7);
        assertEquals(107, result);
    }
}

JUnit , assertEquals . .

, , , , . , - , specialAdd - , specialAdd.

+1
  private static int specialAdd (int a, int b) {
      if(a < 100) {
        return 100 * a + b;
      } else {
        return a + b;
      }
    }
  public static void main(String[] args) {
    System.out.println("" + specialAdd(10,20));
    System.out.println("" + specialAdd(11,21));
    System.out.println("" + specialAdd(12,23));
  }

. , . java " " → Java-. , , .

+1

Junit :

@Test 
private void specialAdd () {
  // provide the value of a and b
  int a = 30;
  int b = 20;
  if(a < 100) {
    System.out.println("100 * a + b =" + 100 * a + b);
  } else {
    System.out.println("a + b =" + a + b);
  }
}

"specialAdd" , run → run junit test.

0

, testng/junit, , , .

But if you want to test it simply with main, you can do the following:

 public static void main(String args[]) {
       int number = specialAdd(99, 1);
       if (number != 9901) {
            System.out.println("Expected: " + 9901 + " Actual: " + number);
       }
       if (args.length >= 2) {//with system args
           int number1 = Integer.parseInt(args[0]);
           int number2 = Integer.parseInt(args[1]);
           number = specialAdd(number1, number2);
           if (number != ...) {} 
       }
 }
0
source

All Articles