Put two developers in the same room and they will argue for weeks about how to best do things. Ask the internet and you will get very diverse answers. Mocking is very good for some things, not so good for other things… the same goes for different kinds of tests. Read different arguments and try to make up your own mind and try them out. Unfortunately I don’t have any good pointers that are up to date… Answer from hrm on reddit.com
🌐
JDoodle
jdoodle.com › online-java-compiler
Online Compiler and Editor/IDE for Java, C, C++, PHP, Python, Ruby, Perl - Code and Run Online
JDoodle is an Online Compiler, Editor, IDE for Java, C, C++, PHP, Perl, Python, Ruby and many more. You can run your programs on the fly online, and you can save and share them with others. Quick and Easy way to compile and run programs online.
🌐
W3Schools
w3schools.com › java › java_compiler.asp
Java Online Compiler (Editor / Interpreter)
Practice is key to mastering coding, and the best way to put your Java knowledge into practice is by getting practical with code. Use W3Schools Spaces to build, test and deploy code.
Discussions

How and where to learn how to properly do testing in Java?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
7
15
November 14, 2023
Testing Function Output In Java
Hi. I’ve just gone through appendix A.4 in Think Java, a book that @P1xt recommended, and I’m trying to test the output of the Convert function as the text directs. Towards that goal, I cloned the ThinkJavaCode repo into my computer, entered the ch03 folder, and compiled the Convert file, ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
January 10, 2017
java - How to write a Unit Test? - Stack Overflow
I have a Java class. How can I unit test it? In my case, I have a class that calculates a binary sum. It takes two byte[] arrays, sums them, and returns a new binary array. More on stackoverflow.com
🌐 stackoverflow.com
Modern Best Practices for Testing in Java
Instead, I suggest focussing on integration tests. By “integration tests” I mean putting all classes together (just like in production) and test a complete vertical slide going though all technical layers (HTTP, business logic, database). Exactly. I have been advocating this for a while now. The same approach is discussed in famous Ian talk - 🚀 DevTernity 2017: Ian Cooper - TDD, Where Did It All Go Wrong . P.s. AssertJ rocks! More on reddit.com
🌐 r/java
50
242
July 14, 2020
🌐
TestDome
testdome.com › tests › java-online-test › 24
Java Online Test | TestDome
The Java online test assesses knowledge of programming in the Java language and commonly used parts of the Java Class Library. This test requires solving live coding problems in Java.
🌐
Visual Studio Code
code.visualstudio.com › docs › java › java-testing
Testing Java with Visual Studio Code
November 3, 2021 - You can also find the command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) by searching for Java: Go to Test. There are other testing commands (for example, Run Tests in Current File) that can be found by searching for 'Test:' ...
🌐
CodingBat
codingbat.com › java
CodingBat Java
CodingBat code practice · Welcome to Codingbat. See help for the latest. Java Example Solution Code · Java String Introduction (video) Java Substring v2 (video) Java String Equals and Loops · Java String indexOf and Parsing · Java If and Boolean Logic · If Boolean Logic Example Solution Code 1 (video) If Boolean Logic Example Solution Code 2 (video) Java For and While Loops ·
🌐
Isograd-testingservices
isograd-testingservices.com › EN › java-pre-employment-test
Free online Java test | Isograd Testing Services
Free Java test and online pre-employment test. Java coding skills assessment.
Address   329 NE Couch Street, Portland, OR 97232, USA
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › compilers › online-java-compiler.htm
Online Java Compiler & IDE - Write, Run & Debug Java Code
Write, compile and run Java code online for free. Features debugging, code sharing, examples and no installation required.
🌐
IBM
ibm.com › docs › en › app-connect › 11.0.0
Testing Java code
After writing Java code to modify pattern instances, test the code to check that it works correctly.
🌐
Reddit
reddit.com › r/learnjava › how and where to learn how to properly do testing in java?
r/learnjava on Reddit: How and where to learn how to properly do testing in Java?
November 14, 2023 -

I've read on r/java that it's not good to use mocking at all and many other claims about best testing practices, so I wonder what are the best resources (preferably books and high quality blog posts) on testing in Java? From simple stuff like Unit Tests, to Integration and End-to-End tests? Are there good OSS projects that are covered with all three kinds of tests and who are the go-to creators when it comes to these kind of things? Also I'd like for resources that are new, up par with current best practices and such, I did look for them myself, but had trouble finding anything worth while. Tried asking this on r/java, but my post got removed, even though testing is vastly talked about, for some reason it didn't fit well over there. Thanks in advance to everybody.

Top answer
1 of 7
11
Put two developers in the same room and they will argue for weeks about how to best do things. Ask the internet and you will get very diverse answers. Mocking is very good for some things, not so good for other things… the same goes for different kinds of tests. Read different arguments and try to make up your own mind and try them out. Unfortunately I don’t have any good pointers that are up to date…
2 of 7
5
In my opinion, testing at all levels is valid. Testing is there for many reasons: document the code usages protect important functionality from being inadvertently broken endure that new code functions Ideally, tests should do some or all of the above. Now testing strategy is where things get harder. The more of your stack that is covered by tests, the slower the tests can be. Also the more unreliable they may be. For tests to be useful, they have to be fast and reliable. So traditionally people recommend the testing pyramid. Have most of your tests at the fast and close to the code level. Then have fewer tests going up the stack. That approach works well, but I have also heard of different test shapes. Testing trophy etc. Here’s my advice: test at every level the bulk of your tests should be at the highest level that is still fast. If you run a trading system with heavy performance needs, then your whole stack better be fast and you can write integration tests all day, maybe mocking out some data feeds to get reliability. If you run a typical enterprise app, maybe your tests should be at the unit level, heavily using no mockito. Above all: tests should be useful. Fast and reliable. If they aren’t useful they’re a waste of time.
🌐
NextLeap
nextleap.app › online-compiler › java-programming
NextLeap - Online Java Compiler
Experience the power of Java with NextLeap's Java Online Compiler. Write, compile, and debug Java code online in real time.
🌐
HackerRank
hackerrank.com › domains › java
Solve Java Code Challenges
Java · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Status · Solved · Unsolved · Skills · Java (Basic) Java (Intermediate) Problem Solving (Intermediate) Difficulty ·
🌐
OneCompiler
onecompiler.com › java
Java Online Compiler
OneCompiler's Online Java Editor helps you write, compile, run and debug Java code online. The code runs on latest JDK & JRE 17
🌐
freeCodeCamp
forum.freecodecamp.org › t › testing-function-output-in-java › 74581
Testing Function Output In Java - The freeCodeCamp Forum
January 10, 2017 - Hi. I've just gone through appendix A.4 in Think Java, a book that @P1xt recommended, and I'm trying to test the output of the Convert function as the text directs. Towards that goal, I cloned the ThinkJavaCode repo i…
🌐
CoderPad
coderpad.io › languages › java
Java Online IDE & Code Editor for Technical Interviews
September 5, 2025 - import org.junit.*; import org.junit.runner.*; public class Solution { @Test public void testNoop() { Assert.assertTrue(true); } public static void main(String[] args) { JUnitCore.main("Solution"); } } Code language: Java (java)
🌐
CodeChef
codechef.com › skill-test › basic-java
Java Online Test & Quiz
Take this Java Online Test & Quiz to verify your practical knowledge and prepare for industry. Get instant recommendation on what concepts to learn after you finish the test. The test will have around 30 problems and will finish in 2 hours.
🌐
Programiz
programiz.com › java-programming › online-compiler
Online Java Compiler - Programiz
// Online Java Compiler // Use this editor to write, compile and run your Java code online class Main { public static void main(String[] args) { System.out.println("Try programiz.pro"); } } Output ·
🌐
Codecademy
codecademy.com › learn › learn-java
Java Tutorial: Learn Java Programming | Codecademy
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more. 4.4 · 10,488 ratings · This course includes · AI assistance for guided coding help · Projects to apply new skills · Quizzes to test your knowledge ·
Rating: 4.4 ​ - ​ 10.5K votes
🌐
Online Java
online-java.com
Online Java - IDE, Code Editor, Compiler
Build and Run your java code instantly. Online-Java is a quick and easy tool that helps you to build, compile, test your programs online.
Top answer
1 of 5
145

I provide this post for both IntelliJ and Eclipse.

Eclipse:

To add unit tests to your project, please follow these steps (I am using Eclipse in order to write this test):

1- Click on New -> Java Project.

2- Enter your project name and click on finish.

3- Right click on your project. Then, click on New -> Class.

4- Enter your class name and click on finish.

Then, complete the class like this:

public class Math {
    int a, b;
    Math(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int add() {
        return a + b;
    }
}

5- Click on File -> New -> JUnit Test Case.

6- Check setUp() and click on finish. The method setUp() will be the place where you initialize your test.

7- Click on OK.

8- Here, I simply add 7 and 10. So, I expect the answer to be 17. Complete your test class like this:

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class MathTest {
    Math math;
    @Before
    public void setUp() throws Exception {
        math = new Math(7, 10);
    }
    @Test
    public void testAdd() {
        Assert.assertEquals(17, math.add());
    }
}

9- Right-click on your test class in package explorer and click on Run as -> JUnit Test.

10- This is the result of the test:

IntelliJ: Note that I used IntelliJ IDEA community 2020.1 for the screenshots. Also, you need to set up your JRE before following these steps. I am using JDK 11.0.4.

1- Right-click on the main folder of your project: -> New -> Directory. You should call this 'test'. 2- Right-click on the test folder and create the proper package. I suggest creating the same package names as the original class. Then, you right-click on the test directory -> mark directory as -> test sources root. 3- In the right package in the test directory, you need to create a Java class (I suggest to use Test.java). 4- In the created class, type '@Test'. Then, among the options that IntelliJ gives you, select Add 'JUnitx' to classpath. 5- Write your test method in your test class. The method signature looks like this:

@Test
public void test<name of original method>(){
...
}

You can do your assertions like below:

Assertions.assertTrue(f.flipEquiv(node1_1, node2_1));

These are the imports that I added:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

This is the test that I wrote:

You can check your methods like this:

Assertions.assertEquals(<Expected>,<actual>);
Assertions.assertTrue(<actual>);
...

For running your unit tests, right-click on the test and click on Run .

If your test passes, the results will look like this:

2 of 5
141
  1. Define the expected and desired output for a normal case, with correct input.

  2. Now, implement the test by declaring a class, name it anything (Usually something like TestAddingModule), and add the testAdd method to it (i.e. like the one below) :

    • Write a method, and above it add the @Test annotation.
    • In the method, run your binary sum and assertEquals(expectedVal,calculatedVal).
    • Test your method by running it (in Eclipse, right click, select Run as → JUnit test).

      //for normal addition 
      @Test
      public void testAdd1Plus1() 
      {
          int x  = 1 ; int y = 1;
          assertEquals(2, myClass.add(x,y));
      }
      
  3. Add other cases as desired.

    • Test that your binary sum does not throw a unexpected exception if there is an integer overflow.
    • Test that your method handles Null inputs gracefully (example below).

      //if you are using 0 as default for null, make sure your class works in that case.
      @Test
      public void testAdd1Plus1() 
      {
          int y = 1;
          assertEquals(0, myClass.add(null,y));
      }
      
🌐
Coderanch
coderanch.com › t › 634976 › java › Running-test-file
Running a test with a file (Beginning Java forum at Coderanch)
June 11, 2014 - Or you can run them manually in Eclipse by choosing Run -> Run As -> JUnit test. If you want, you can post some code here and I will try to answer your questions. All things are lawful, but not all things are profitable. ... OK... I have a problem with Juint so I'm publishing my code. ShapeDimensionCalculatorTest.java: Shape is an interface, and Rectangle, Ellipse and Square implement it.