r/javahelp • • Jul 24 '21

Unit testing with Mockito framework... Why do we actually have to do it? What's usually the point?

Sorry if this is dumb but I just got a job and introduced to my first assignment yesterday. The task at hand was to implement unit tests using the mockito, powermockito, and junit framework for about 4 classes, each with about 300 lines, to be done by end of day Tuesday. I have 2 yeo but I've never done unit tests before, and I really don't get what the purpose of them is, but I was told the assignment was given "so that I can better understand what's going on within the codebase." While this makes sense, what I'm finding is that I'm more struggling to learn about what unit testing actually is, what I'm supposed to test, and what the actual point of it is. It's also a bit interesting because people with less software dev experience and exposure are introducing this to me, and I'm really not getting what the concepts are for - it seems like a special type of programming that I've just never seen, but also seems arbitrary to some degree, but it also seems simple for most devs just getting introduced to all of it.

So I go into one method, and I test it out. It's like a response.getContent(), and I do when(respones.getContent()).then(mock(Content.class)) or something, and then I'd go into the code and behave as a compiler testing out each of the individual components. But... obviously they all work. They all have return types that work, and that behave as actual classes. But then there are if statements to test, for loops, array lists, and it seems as though JUnit testing doesn't deal with most of that stuff until you get to an actual method call. Then once you get to that method call, you go into that method, and you write tests for that method in the function that has an actual @Test annotation. Do I have that right? Anything I'm missing?

I'm just unsure as to what the conventional wisdom is behind unit testing. If something were to break, I could test it manually, or test an endpoint in Postman, but these unit tests seem like they're made up, or that the only point of them is to just "become the compiler" and better understand the code that way. Even the other unit tests I've seen in this project seem really hand-wavy, and don't even seem to test everything. Then it would also seem that unit tests don't serve any inherent purpose, since they're static, and you can't see what's going on within the code in a more dynamic setting, like when it's live.

Could someone help my understanding? I am having some trouble understanding what value unit testing actually brings. Thanks.

19 Upvotes

27 comments sorted by

•

u/AutoModerator Jul 24 '21

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

  • 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.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

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: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) 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.

13

u/ColdFerrin Jul 24 '21 edited Jul 24 '21

The general goal of unit testing is to make sure that each individual part of the code is correct. Usually, a unit test is to test one function, class, or unit of code. The general idea is that the unit tests provide a contract that the code is correct, this means that if the code needs to change, then you are still guaranteeing that the overall unit is correct, in that the unit tests still pass.

The other thing, in a correctly managed design process, is that the unit tests are generally written before any actual code is written. You are supposed to design all of your classes, write tests for the classes, and then implement the classes. This means, that when a test fails, either your test did not take something into account, or the code that it was testing has a bug. It is possible to do the unit tests simultaniously, or after, but then you can be put into a situation where the unit test can be passing but the code that it tests is actually wrong, and then that defeats the point of having the test at all.

3

u/INFLATABLE_CUCUMBER Jul 25 '21 edited Jul 25 '21

Reading all of the responses is actually making me a bit more confused and kind of concerned… for context, this company I’m working at is a big bank known for good tech. I would’ve assumed the dev lead who assigned these projects has a good idea of where this is going, but the fact that so many people are like “wait, you’re writing tests after already writing the code?” has me sort of weirded out. It was also an extremely large assignment that would have me go through a lot of the already written code and write these tests for them line by line for every function, acting like a compiler, and I’m not sure I understand now why these tests are needed. Could it just be house-keeping coming from someone at the top, who said “we all need to do unit testing now,” or is there an actual structural reason as to why this assignment exists, other than to get acquainted with the codebase? I would assume there is a good reason.

3

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jul 25 '21

Reading all of the responses is actually making me a bit more confused and kind of concerned… for context, this company I’m working at is a big bank known for good tech. I would’ve assumed the dev lead who assigned these projects has a good idea of where this is going, but the fact that so many people are like “wait, you’re writing tests after already writing the code?” has me sort of weirded out.

That lead is just giving you these assignments because he knows you're incredibly inexperienced and need something easy to learn unit testing and learning the code base at the same time.

Really, you (and others) should not just go assume that lead dev is wrong. You should focus on all the stuff you really need to learn.

I would assume there is a good reason.

Yes. Unit testing is a mandatory skill, period. That's why they're giving you this assignment. And frankly you should be glad you're even in this position.

1

u/INFLATABLE_CUCUMBER Jul 25 '21

So, uh, if my unit tests aren’t the greatest or don’t cover the entire class that’s given, should I be worried at all? Or is this assignment more about just ensuring I get familiarity with the codebase? I would like to assume that I’m relatively safe in case I fuck up, but I also don’t exactly want to ask too many questions about something as relatively simple as this to not come off as a dumbass.

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jul 25 '21

Use a test coverage tool to see if you missed anything important.

4

u/AreTheseMyFeet Jul 25 '21 edited Jul 25 '21

the fact that so many people are like “wait, you’re writing tests after already writing the code?” has me sort of weirded out

All of those replies are for what we'd consider the "ideal case", or "best practice". The Test Driven Development (TDD) ethos is what these commenters are describing which is certainly a worthwhile approach to development but it (as most "pure" or "opinionated" paradigms do) often runs in to problems with the real world - time constraints, pressure for short-term features rather than long-term stability, initial requirements were only for a MVP (Minimal Viable Product) version, taking over a (poorly?) managed product late in the development, management that won't understand/sign off on the time sink, postponed test maintenance as code evolves and lots of other reasons mean that sometimes code bases just don't have well maintained tests.

As a new developer to any team/project getting your hands dirty on the testing side has some good dual purposes/benefits - you get to know the code base and project domain well, they get to see your skills in action applied to a low(er) risk section of the project and the project itself gains improved/extra tests which makes it on the whole, more robust.

So in a perfect world all projects would always have tests written first (if you ascribe to the TDD ethos) but reality and pressures from multiple directions means that there isn't always the time or priority for it. You shouldn't be too worried about finding skeletons in the closet, so to speak, every project of any size or complexity has them - perfect code bases are like unicorns, it'd be great if they existed but sadly the real world says nope.

1

u/[deleted] Jul 25 '21

[removed] — view removed comment

0

u/Louisblack85 Jul 25 '21

I’d agree if the person writing the tests actually knew about unit testing. Writing good unit tests for legacy code is difficult at the best of times. If they don’t understand the point of unit tests, then you’re going to end up with some awful tests!

2

u/AreTheseMyFeet Jul 25 '21

You have to start them somewhere. If you give out this work (improve/validate unit tests) to a new dev who doesn't have any experience with unit testing I'd hope that developer would share that fact and we can then find what material/training/skills they need to get going on the task. As their senior I'd find a couple of good tutorials aimed at their experience level and tell them to go through it all and come back with any questions later. Nobody expects new hires to know everything about everything and that goes especially so for recent graduates or others with no industry experience under their belts yet. Good management/colleagues will know this and have expected it.

3

u/steave435 Jul 25 '21

acting like a compiler

This implies that you're testing the wrong things. You should not be testing Java, you should be testing the logic you wrote in it. If the compiler would detect it, you don't test it.

What you're supposed to do is send some inputs to the methods, and assert that the response matches what you'd expect. Make sure to include some inputs that you normally wouldn't expect but that it should still be able to handle, and that you test inputs that would produce different outputs, so you don't have your tests going green because there's a bug that makes it always return the same value, which happens to match your test case.

2

u/morhp Professional Developer Jul 25 '21 edited Jul 25 '21

go through a lot of the already written code and write these tests for them line by line for every function, acting like a compiler

You're not supposed to test the compiler. Your job is to find out what the functions are supposed to do and check if they do exactly that.

Assume you're supposed to test the java function String.split(). The function is already written and you could look at the code, but your main job would be to figure out all sorts of standard and weird/edge cases and write tests for them.

This will also help people understand the code and problems with it. Could you say what ",,ab,c,,,".split(",") returns? Make a guess, write a test for that, and if it doesn't match the actual result, either your guess was incorrect or the Java implementation of split(). If the function intentionally behaves "weird" in some cases, your tests will help documenting that.

5

u/[deleted] Jul 25 '21 edited Jul 25 '21

Just want to add some examples of this excellent explanation.

The other thing, in a correctly managed design process, is that the unit tests are generally written before any actual code is written.

I like to think that tests are low level requirement specification.

The general idea is that the unit tests provide a contract that the code is correct, this means that if the code needs to change, then you are still guaranteeing that the overall unit is correct

Let's say you're working on a basic calculator, specifically on multiplication, so (two of) your low level requirements are like

  • The product of two positive numbers is positive

  • The product of a negative number and a positive number is negative

So you write your tests

void productOfPositives(){
      
     assertThat(myMultiplication(2, 3)).isGraterThan(0);

}

void productOfPositiveAndNegative(){
      
     assertThat(myMultiplication(2, -3)).isLesserThan(0);

}

And all works.

But, next month, someone finds out that for some (imaginary) reason working with loops to add numbers is faster, so he replaces your multiplication a * b with a loop

int result = 0;

for(int i = 0; i < b; i++){
    result += a;
}

Now, this will break your tests, because this new approach has issues with negative numbers. And, by breaking your tests you know this change need more work, or even discarded.

I know this is a silly example, but it states what I think are the most useful aspects of unit testing, assuring your code satisfies your low level requirements and guarding you against future changes that might break your code.

Now, about mocks.

Most of the times we work with layered systems. So a controller has a service and a service has a repository (on the most basic example).

Let's say your service must retrieve a list of elements from the repository and do some calculations.

You really want to test these calculations, but, since unit testing is isolated, you won't have a real repository, so you will have a NPE as soon as you try to run your code. To prevent this you mock all your dependencies, passing objects that behave like the real ones, but are dummy objects. You can even configure your mocks to return values for a given input.

I would recommend this book on the subject.

Edit. Typos

10

u/[deleted] Jul 24 '21 edited Jul 24 '21

The point of unit testing is to test business logic. There are also integration testing which tests how the code integrates with other components (such as databases) and functional tests (which test the system end-to-end).

But often times, you'll come to a boundary, where you are interfacing with another system. For example, getting data from a database or updating the database. You don't really care what the database is doing (you'll test that with an integration test or a functional test), but you need to get canned data into the system. Or, you want to verify that the business logic is saving the right data.

However, there is a principle called "don't mock what you don't own". You should be defining your boundaries with interfaces that define what you expect from the component on the other side (such as the database). Think of it as a service contract for a third-party dependency. You should never mock things like JDBC Connection, Statement, ResultSet or the JPA EntityManager. You should instead define a Repository interface and mock against that.

If you find yourself writing complex or seemingly pointless mocks, that is a sign that something is very wrong with the design.

7

u/CrackedRose99 Jul 25 '21

“But... obviously they all work. They all have return types that work, and that behave as actual classes”

I cannot figure out what you mean by that.

For each method you pass in known values and check that you get the expected values back.

Mockito allows testing a method without needing the methods that the method depends on.

Arrange unit tests in reverse call tree order. Pass results from each test to the upcoming tests.

Unit testing usually does not catch much initially if you have written your code carefully.

When there is a bug, unit testing will pretty much lead to you directly to the issue. Other testing tells you there is an issue, but not where it is. Unit testing allows you to see what would fail if a given set of input was provided.

4

u/khooke Extreme Brewer Jul 25 '21

If you're being asked to write new tests after someone else already wrote the code, you've already missed (most) of the the benefits of unit testing:

- unit tests help you know your code is working as you write the code. Whether you subscribe to TDD and write tests first or not, being able to write some code, test your code and repeat helps you know your code is working as you go

- unit tests help test the smallest parts of you code in isolation from each other, If each of your smallest parts work in isolation, it's more likely they'll work together as you combine them together to construct your system

- unit tests help you regression test your code. If you have an enhancement to make and the code already has tests, you can run the tests prior to the change, make your change, then rerun the tests again to get a degree of confidence that nothing else was broken as a result

- unit tests are a key concept to enabling CI/CD. As you build your code then run your tests, you use them as a quality gate to decide whether you should deploy or not. Passing tests = good to go. Failing tests block your deployment

All that said, there is a massive difference between tests that are useful, and those that are not. If your tests test things that are not relevant, or don't effectively test that your code works, then you don't get any of the benefits above.

2

u/denialerror Jul 25 '21

If something were to break, I could test it manually

The point of automated testing, whether unit tests or any other sort, is to know something is broken before going out to the users. You don't have to wait for something to break in production to find out there's an issue.

2

u/de_vel_oper Jul 25 '21

We had an issue on Friday where the customer said the app had broken. Under investigation the app had no unit testing and someone had altered the code.

The test can do two things 1). Check for breaking code changes

2). isolate where your problems lie.

1

u/darkecojaj Jul 25 '21

Big benefit with unit test is this:

  1. You avoid silly mistakes by writing test code to verify methods work in a variety of conditions.

  2. You or your teammate's changes verify the functionality of the code still works, especially with the additional or modification of a feature.

  3. It can help give an idea of what certain functions should and should not do when skimming over the code to try to understand it.

1

u/Fenxis Jul 25 '21

Unit tests have one big advantage over end you end testing... It's much easier to write tests that conver a whole range of inputs. A well written unit test can provide a solid description of the interface provided.

In more complex code environments it also protects your code from defects arising from refactoring and other large changes.

Finally it's not practical to always manually test. The developer may not know how to test the component properly. It also forces the validation of the software as a mandatory part of the build process.

Idk if you need to write the tests first but.. it might affect how you write your code(hard to take short cuts). Also if you have Interface driven development you might be able to use dummy implementations over mocks (due to computational cost)

1

u/Efficient-Society-11 Jul 25 '21

This happens in most companies. Everybody wants to show that they follow TDD but they end up doing it the wrong way. Ideally like what others said in this thread, we need to write the tests first and then write corresponding business logic. But this doesn’t happen 99% of the time. So what you are asked to do is pretty common. It is very difficult to grasp the concept of unit testing. Sometimes I also wonder that I am mocking the result in the test, so obviously it is going to return the same thing when the output method is called in the test. For now, just go ahead with it without trying to make more sense because it is not going to make any sense frankly. Just make sure in the future projects when you write new code you follow it the way TDD should be done. Thanks!

1

u/Danji1 Jul 25 '21 edited Jul 25 '21

Good luck knowing the code still works in a few years time when 5 other teams got their hands on it and added all kinds of shit to it. Would you be so confident then?

Enterprise software development is not a one-man band. Most the the time you will have to work with code that you didn't write and know very little about. Your easiest way of knowing whether the code still works as intended after your changes is to have unit tests testing the underlying logic.

1

u/ZukoBestGirl Jul 25 '21

Fair warning

I went through your post multiple times in this answer. But I've done my best to structure it in order of importance. The first third is way more relevant than the ending two thirds. I also repeat myself so that I express how important certain things are. I haven't offered any TL;DR, but if you want one, again, the first third. Good luck!


but I've never done unit tests before, and I really don't get what the purpose of them is

There are multiple types of test, but the most basic idea is unit test. Where you take some code, a class, a service, a method. And you test it in isolation.

Let's say you have a service that, once called from a Rest Endpoint, it reads from a DB, turns that Entity into a json, and spits it out at the caller. Easy peasy lemon squeazy.

So what would such a Unit Test entail? Personally, I see two scenarios here. A real unit test, that only tests your service method directly. Or a mini integration test (or whatever, I ain't no expert) that mocks an endpoint call with MockMvc. Honestly I'd go for the latter 90% of the time. Unless this is a general purpose service method that can be used in many contexts, you want to know that no one broke your endpoint. Since in this example the endpoint and the service are synonymous, test them both at the same time.

Now, you don't care about the databse. You don't care what type, where, what connection, if the data actually exists or not. It's all irrelevant. You wanna test the service in two cases:

  1. Data exists.

  2. Data does not exist.

You mock your DB, and tell the mock: "When asked about this, just spit out a pre-defined value." One for good, one for bad.

Then test the output of the service / endpoint. Assert that on good data, gives good response. On bad data, expected bad response.


What is the point of this?

If something were to break, I could test it manually, or test an endpoint in Postman, but these unit tests seem like they're made up, or that the only point of them is to just "become the compiler"

 

I will be deconstructing this sentence multiple times, bare with me.

Yes, you can test something manually. Like a caveman. Or, you can have a much more reliable machine do it for you. Automatically, every time some idiot intern pushes code to the remote.

You don't have one test, you have a thousand tests. If any one test, a single one, fails ... then that code gets automatically rejected. Does not end up on the remote. Does not break the build.

Instead of manually testing, like a caveman, the machine does it for you. And it test everything. Not weeks after something has broken, and someone has reported a defect, but before the code even gets anywhere even remotely important.

Each and every time somone changes anything, the tests run. If they are well written, it is nigh impossible to break a working system. The machine won't let you. Whereas humans are stupid. They will break hundreds of things and have no idea they did anything wrong.

Could you like ... give me an example?

Sure, but maybe a contrived one. Lets say, we have implemented some UI thing. IDK what. Let us say we have all sorts of menu items in the application. And business objects. And menu items relate to business objects. But whomever implemented this, had no idea, or wasn't smart enough, or didn't care.

We see that we need to expand the application and grow. We are hiring 50 new devs over 2 years. The application will be huge. But to make the transition easier, and not waste time, we need to refactor all our context menues. So that they are generic, dynamic, business objected related, and so on and so forth.

Without tests, after we refactor everything, we have literally no idea if it works. We have to test each and every menu, in all possible contexts, to see if it worked. Who know what could have breaked, where and when. If you don't test a single menu, I guarantee, that is the menu you'll have the most bugs in. Poe's law reigns supreme.

But if you have proper tests in place. You need not worry a single thing. You do your refactor, re-design, re-implementation. And then run the tests. If it's green, everything is fine. Move on and do something worth while.

Back to your statements

and then I'd go into the code and behave as a compiler testing out each of the individual components. But... obviously they all work.

 

OFC they work, if they didn't work, why even write the test? Your number #1 problem is that the thing you just implemented doesn't work.

But. Will it work a year from now? How about if it depends on some component, and a year from now, a junior dev has a defect on that underlying component and decides to change it in a fundamental way. Sure, it solves his bug. But will your code still work?

Ofc a test for a freshly written component passes, what else should it do?

But that's not the point. The point is that the test will run, each and every single time someone pushes code to the remote repo. If that test fails, because whatever reason, you have an early warning system that there's a problem.

 

and then I'd go into the code and behave as a compiler

 

Never. You don't test java. You don't test a string split. You don't test an Integer.MAX_VALUE. No. You test, that what you just implemented, produces the exact output you expect it to, based on your pre-defined, mocked, input.

That is why you see stuf like: when(db.call(any())).return(myExpectedValue). Because you're not testing your DB. You just want some input. But your service doesn't take input as a parm, but rather has an internal piece of code that calls the DB. You don't care about the tb, so you say, and I'm paraphrasing:

"When whomever, I don't care, is calling some method, with ... whatever input - I don't care - just return this predefined value plz, so I can test my thing. Tyvm."

 

these unit tests seem like they're made up

 

Maybe they're bad tests, IDK

 

or that the only point of them is to just "become the compiler"

 

Never. Don't test java. Don't test jaxb, don't test a db connection. Test your code. Input / output. Expected behaviour, exceptions, exception handling.

 

They all have return types that work, and that behave as actual classes. But then there are if statements to test, for loops, array lists, and it seems as though JUnit testing doesn't deal with most of that stuff until you get to an actual method call. Then once you get to that method call, you go into that method, and you write tests for that method in the function that has an actual @Test annotation. Do I have that right? Anything I'm missing?

 

Let's break this one up also:

 

They all have return types that work

Are you testing .... a return type? How? If a method with a return type would try to return something different, it wouldn't compile. What's there to test?

 

But then there are if statements to test, for loops, array lists, and it seems as though JUnit testing doesn't deal with most of that stuff until you get to an actual method call.

 

You don't test java. You don't test an if. You test a metod. Now that method might have an if, and inside an if or an else, it calls another method. That is something test-worthy.

For an input of X, it should enter if/else block #4, and call the method whatever 1 time.

verify(mockedObject, times(1)).someMethod("This method was called exactly once");

If the code changes, and it no longer calls this method. Or it calls it twice. Test fails

But, why should it fail?

Because, you test design. If for that input, it was designed to call that method once. No longer doing that is an error.

Sometimes tests can cahnge. But only if the requirements or fundamental design changes.

But you can easily see this. Do a design change, run all your tests. And you will have 1 of 3 reactions:

  1. All good

  2. Hmmm, this thing that breaked, was unexpected. I did something bad

  3. Ah yes, ofc this breaks, because now the code does something fundamentally different and this test is no longer relevant in its current form.

 

and you write tests for that method in the function that has an actual @Test annotation.

 

@Test does only one thing. It treats that method as an entry point for a test. It calls it in a test suite, displays the test result next to the method name. So these methods should have very relevant names. Or there are annotations to change the displayed name.

If no method has a @Test, nothing will be called by the framework. If you, in a @Test method, call an aditional method utilityGetSomething(X), then that method is called within the parent test. If anything fails in your utility method, the test that called it fails.

Don't think there's more to say on this topic 🤷‍♂️

 

I'm just unsure as to what the conventional wisdom is behind unit testing. If something were to break, I could test it manually

 

The point is that tests are smarter than you are. They will break, before you have any idea whatsoever that something is wrong. Well, this is only true if you have well written tests.

There is a lot to say on tests. I haven't even touched integration tests, which are the oposite of unit tests, in a sesnse. But I already wrote too much and risk burying you in details. GL!

1

u/ElFeesho Jul 25 '21

Lots of great content in here, what I'll add is: Don't try and understand by justifying to yourself why you don't need tests, try to understand by justifying why you do need tests.

Give yourself the opportunity to take buy into the concept of having well tested code.

If I was given a legacy project and it had 0 unit tests and I was asked to change it some way, how would I know what I had done didn't break anything?

If I add functionality to a project, what's the best way for me to stop someone from breaking it in the future when I'm not around?

Continuous integration systems run automated tests as part of producing a deployable artifact. This means if someone breaks a test (because they broke the behaviour of the system) it won't be shippable to production.

It's all about reducing the size of your feedback loop.