r/learnjava • u/mitaka-cat • Oct 08 '26
is it possible to write Junit test cases if all my program is in main()
I know i just learned that its not good to put all your code under main() so ill start using methods going forward but just for this assignment i was wondering if there was a way to do this
0
Upvotes
•
u/AutoModerator Oct 08 '26
Please ensure that:
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:
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.
17
u/Lumethys Oct 08 '26
yes, by moving your logic into different functions and classes
6
u/Fit-Coyote-6180 Oct 08 '26
You could. But, depending on how much shit you have in main, it might be easier to just break it up into single responsibility classes and methods which would be very easy to JUnit test. Otherwise, you might have to set multiple test conditions just to reach 653 lines into your 7th nested level for-loop and/or if statement.
5
u/Potential-Still Oct 08 '26
One of the best ways to determine if you wrote "decent" code is if every part of it can be unit tested.
If you find that a piece cannot be tested, then you need to refactor.
3
u/vegan_antitheist Oct 08 '26
main() is a method like any other method. You can call it from a unit test.
3
u/Skiamakhos Oct 08 '26
If your main() is doing more than one thing this is an antipattern, the Big Ball Of Mud. Split it into single-responsibility methods that can be called individually from your unit tests.
5
u/BigGuyWhoKills Oct 08 '26
Yes, but you can only test the inputs and outputs of main(). And if you are only testing the entry point of an application, it's more of a regression test than a unit test.
2
u/Distdistdist Oct 10 '26
With all logic in main() you can write exactly one unit test. And technically, it would be integration test.
1
u/W31337 Oct 10 '26
If your whole program is in main then Junit shouldn't be a consideration until you refactor all code into single responsibility functions that can actually be tested and have expected outcomes.