3

I'd like to quickly check the result of a static method I'm analyzing, within the IDE if possible.

public static int foo(int a, int b){
    return (a & 11) ^ b;
}

For example, right-click the expression and run it, something like

> foo(11, 2)
> 9
> foo(28, 3)
> 11

I don't know the result in advance (the actual code is more complicated), so unit testing is out of the question.

How can I do this?

andrybak
  • 2,129
  • 2
  • 20
  • 40
Jivay
  • 1,034
  • 3
  • 10
  • 23
  • 2
    What is your question? – Korashen May 23 '18 at 08:57
  • I'm sure it's possible in any IDE. For IntelliJ instructions look here https://www.jetbrains.com/help/idea/running-applications.html – Jan Ossowski May 23 '18 at 09:01
  • Possible duplicate of [How to write a Unit Test?](https://stackoverflow.com/questions/8751553/how-to-write-a-unit-test) – AxelH May 23 '18 at 10:05
  • 2
    Eclipse: see [Creating a Java Scrapbook Page](https://help.eclipse.org/oxygen/topic/org.eclipse.jdt.doc.user/tasks/task-create_scrapbook_page.htm) – howlger May 23 '18 at 10:08
  • I don't want to run tests, I want to run a function from a code snippet and get its result. Unit tests imply that I know the result, which I don't in my case (not this code which is over-simplified). Also I can't run the application, so I can't [evaluate an expression](https://www.jetbrains.com/help/idea/evaluating-expressions.html) either. My only option is static analysis and running snippets like this. @howlger I think you found it. Also on Intellij https://stackoverflow.com/questions/11028605/intellij-idea-eclipse-scrapbook-equivalent – Jivay May 23 '18 at 10:14
  • If you're on Java 9, the `jshell` might work for you too. – Evan Knowles May 23 '18 at 10:26
  • This is indeed not a case of unit testing. But how can you write a method where you don't know the output ? I don't know you, but I am used to have specification to write code, which mean that for specific inputs, an output is provided. Complicated or not, you should already have at least a few test cases. – AxelH May 23 '18 at 10:58

3 Answers3

2

In Eclipse there are the so-called Java Scrapbook Pages: see Eclipse Help - Creating a Java Scrapbook Page

Since Java 9 there is the so-called JShell which can also be used without an IDE: see Oracle Help Center - Introduction to JShell

Java 9 JShell can be used from IntelliJ IDEA: Tools | JSHell Console... action.

Community
  • 1
  • 1
howlger
  • 31,050
  • 11
  • 59
  • 99
1

If using IntelliJ and Java 9+, you can access jshell, the repl that comes with Java 9 and above. That allows you to paste your method definition into the jshell console and then run it with different parameters.

See https://blog.jetbrains.com/idea/2017/09/java-9-and-intellij-idea/ for more details (the actual JShell section is halfway down, and apparently has no direct link)

matt freake
  • 4,877
  • 4
  • 27
  • 56
  • You still need to copy past the method to `jShell` ? So there is no added value using intellij. They just provide a shortcut to `jshell`. That's too bad. It would have been useful that it "pre-load" the method instead of having to copy-paste it. – AxelH May 23 '18 at 11:06
  • @AxelH the advantage is that it sets the classpath of your project. – Tristan Tarrant Mar 03 '22 at 08:41
1

In Java 9, you can use the jshell as mentioned in other answers.

If not, you can still use unit tests. Technically, a unit test does not need to have an assertion. You can have any code in your unit test, which means that you can also have something like this:

@Test
public void getMyResult(){
    System.out.ptintln(foo(1,10));
}

Obviously, you shouldn't commit this test into your source code repo as the test is badly designed (it will always pass unless foo throws an exception) but for your purpose, it might do the trick.

Edit:

As @AxelH said in the comments, you can also create a simple main method.

vatbub
  • 2,713
  • 18
  • 41
  • That's not really a @Test. Just create a `main` method then. That would be the same. – AxelH May 23 '18 at 11:07
  • Yeah, you're right. I actually didn't even think about creating a `main` method. I added it to my answer, thanks for the idea :) – vatbub May 23 '18 at 11:11
  • On the other hand, depending on the project/setup, it might be easier to create such a `@Test` than creating a main method. – vatbub May 23 '18 at 11:13