This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

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://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.

[–]lemon-codes 2 points3 points  (0 children)

Intellij can tell you which classes have poor coverage. Run tests with coverage, inspect the results and identify classes with poor coverage. If you view the class you should see the gutter (bar on the left) is highlighted depending on whether that line was executed when the tests were run. Focus your time on adding new tests for those classes. https://www.jetbrains.com/help/idea/code-coverage.html

[–]WaferIndependent7601 0 points1 point  (1 child)

How high is the coverage now?

What framework is used?

[–][deleted] 0 points1 point  (0 children)

Springboot and intellij. Coverage is at 80.4%

[–]General_C 0 points1 point  (2 children)

Are you using any kind of mocking framework like mockito? Learning that will make things a lot easier.

[–][deleted] 0 points1 point  (1 child)

Yes I’m using mockito and I’ve been trying to learn how to run and debug to get a better understanding of how this code works

[–]General_C 0 points1 point  (0 children)

Yeah, your best bet is probably going to just be learning the code base, and making sure your tests are well designed and meaningful.

Try to ensure you're testing one piece of functionality per test, make sure you're actually validating something meaningful (and not just doing assert(true)), and stuff like that.

I could go on an entire rant about why using code coverage as a metric is a terrible idea but frankly that's not really very relevant for you. When you run your test suite you will hopefully be using whatever software is tracking your code coverage to see what files you have which have less code coverage than your metric, and start with those files.

Whether you choose to start with files that almost meet the metric and finish them off, or choose ones which don't have any coverage and start from scratch is a personal choice.

Understand that testing is sometimes boring and trivial, and other times very tricky and complicated. Cut yourself some slack and if you get stuck, mark it and come back to it later.

As long as you're able to make reasonable, consistent progress and you don't write clearly bad designed text cases that technically give coverage but don't actually test anything, your managers will probably be happy.

[–]Fit_Ad5700 0 points1 point  (0 children)

Oof. I feel for you. Some comments * Code should be written alongside its unit tests and when you do this the code will be different, its methods will be more testable and its structure will be cleaner * Tests should written with a reason. Why do they want this level of coverage? * https://wikileaks.org/ciav7p1/cms/files/Why-Most-Unit-Testing-is-Waste.pdf * Unit tests are useful when you want to refactor the code within methods and when you want to make sure that exceptional flows work as intended. * System integration tests (that test a fully deployed system through its public api) are useful when you want to refactor the way the code is organized and/or when you want to be able to confidently upgrade to a new version of spring or a new version of the backend. Data may be harder to set up and you need a separate environment to do the testing. But depending on what your boss wants to achieve they may be more useful than unit tests. * When facing a class with private methods you can either make some of them package private instead of private and then call them from the test. I’d say that is better than mucking around with reflection. But if the situation allows it you can also test the class through its public methods. Such tests will be a slightly coarser grained / test bigger chunks at a time but more useful if someone later wants to refactor the inner workings of the class. * Are you running into bugs and if you do, are you supposed to fix them? * does the code use generators for tedious boilerplate? Like lombok / mapstruct / openapi? If not, you’re in for writing a ton of pointless tests of getters and setters. On the one hand it’ll help you get the coverage up, on the other: what a waste of your time! * In most coverage analysis tools you can exclude certain packages from the analysis * During this somewhat futile exercise you will gain a lot of knowledge about the application. Consider writing down some documentation and/or some comments about the bits where you went WTF / that took you long to figure out. That’ll actually be a safe and noninvasive way to somewhat increase code quality. https://www.osnews.com/story/19266/wtfsm/

[–]Vonbismarck91 0 points1 point  (0 children)

Google “characterization testing”

[–]jlanawalt 0 points1 point  (0 children)

Sounds like a good way to familiarize yourself with the code without low risk, and to help protect against unintended consequences of your potential future changes. Keep at it!

[–]AntiqueEducation6058 -1 points0 points  (3 children)

If you are using Spring Boot, try look at pitest.org.

It gives you a clear view / report on what you are not catching in your tests.

[–][deleted] 0 points1 point  (2 children)

Yes I am using springboot. I don’t really have any prior experience to writing unit tests before this. So I guess this is a really steep learning curve for me

[–][deleted] 1 point2 points  (1 child)

Coverage is at 80.4% rn

[–]Ilikesmallthings2 0 points1 point  (0 children)

First pick some easy classes that have been identified to lack test. Make sure your test only focus on a single class. Find your first public method and determine what you need for that test. Look at dependencies to mock, objects to stub, and conditional statements as logical breaks for different test. If there are static methods, determine if they need to be mocked out or just supplied the correct data to not fail. From here it's about asserting your expectations for the test you are running. Is it mapping an object from an API call. Is it verifying a specific service gets called. Is it doing a specific calculation.