Do I need to write unit tests for methods of services without logic?

unit testing
unit testing guidelines
what is unit testing with example
how to do unit testing
unit testing c#
unit testing principles
unit testing best practices
how to write unit tests

Do I need test service methods like this?

@Transactional
@Override
public Charge saveAndFlush(Charge charge) {
    return chargesRepository.saveAndFlush(charge);
}

In principle, there is nothing to test. Therefore, the question arose - to write a test anyway, because in the future it is possible that someone will add logic here. Or let him write the one who will add logic (if at all).

Generally speaking you focus your unit tests on code that returns a value by performing some computation or processing. You could unit test a method that does not return any value to ensure it's going through without running exceptions while executing all the possible paths. In the case above I believe you should not write a test for it. There's really no logic in that method and you'd be unit testing the spring framework. There's no need for that as the spring framework already has plenty of unit tests. Do notice unit tests are supposed to run fast and must work standalone, that is, a unit test cannot depend on a database or network connection. So that's another reason not to unit test it as you would depend on a database to verify the code worked.

Do I need to write unit tests for methods of services without logic?, The whole class was a manager (or a service if you prefer to call it like that) and But then again, I spend most of my time writing unit tests for legacy code, only Especially as you have already written the test before you get the chance of However, if you are confident that there will be no logic in this method and it's likely  You could unit test a method that does not return any value to ensure it's going through without running exceptions while executing all the possible paths. In the case above I believe you should not write a test for it. There's really no logic in that method and you'd be unit testing the spring framework.

there is nothing to test

Well, there is. Because doing no tests can not distinguish between the correct implementation and the following faulty "implementations":

{
   return null;
}

{
   return chargesRepository.saveAndFlush(null);
}

{
   return chargesRepository.saveAndFlush(new Charge());
}

{
   return chargesRepository.someOtherMethod(charge);
}

But you are right to think there is very little to test. Assuming the class of chargesRepository is already properly tested, you only need one or two unit tests to show that the method correctly delegates to the chargesRepository.

Should we test all our methods?, If your tests are hard to write, you probably have problems elsewhere. We will see that writing unit tests and generating testable code is not just about in a business logic class, and realize that it requires external resources like web services or it impossible to test the internal logic of the GetTimeOfDay() method without  This is a nice idea, but a good chunk of the typical developer's code just doesn't need to be unit tested. Complex business logic absolutely needs to have corresponding unit tests. But writing unit tests for simple logic will require the developer to spend more time writing tests than delivering value to the business. 100% Code Coverage

I found this answer to a similar question:

Kent Beck's rule of thumb:

Test everything that could possibly break. Of course, that is subjective to some extent. To me, trivial getters/setters and one-liners like yours above usually aren't worth it. But then again, I spend most of my time writing unit tests for legacy code, only dreaming about a nice greenfield TDD project... On such projects, the rules are different. With legacy code, the main aim is to cover as much ground with as little effort as possible, so unit tests tend to be higher level and more complex, more like integration tests if one is pedantic about terminology. And when you are struggling to get overall code coverage up from 0%, or just managed to bump it over 25%, unit testing getters and setters is the least of your worries.

OTOH in a greenfield TDD project, it may be more matter-of-fact to write tests even for such methods. Especially as you have already written the test before you get the chance of starting to wonder "is this one line worth a dedicated test?". And at least these tests are trivial to write and fast to run, so it's not a big deal either way.

Unit Tests, How to Write Testable Code and Why it Matters, You can learn, and you don't even have to climb too steep of a hill. run on a machine without the “proper setup,” you haven't written a unit test. If you create some sort of test that throws thousands of requests for a service you've written, You thus write a unit test by writing something that tests a method. A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.

The key reason for this method to exist is the transactional annotation and the side effect on the charges repository. That's also the most likely/only thing to not work as expected. That's something you find out with an integration test. Unit testing this is completely pointless because you'd mock the storage and ignore the annotations. So, you are basically unit testing that a random call on a mock works. Which, surprisingly, tends to mostly work as you'd hope.

So, make sure you cover all relevant repository methods with only a few good integration/scenario tests (i.e. don't write one per method but combine them into realistic scenarios). Focus unit tests on things with algorithmic complexity that have more than 1 line of code or any kind of branches. That way you can mock your DAO class when unit testing your service classes with actual business logic may have real bugs because you already know the DAOs have the desired side effects from integration testing.

Unit Testing Tutorial: 6 Best Practices to Get Up To Speed, acceptable because all invocations are required to make the application work Unit tests can save a lot of time because when you move code from the test context We are referring here to code that you use in your logic without requiring an Services, in which you write custom code, or when writing extension methods  Turn live unit testing from the Test menu by choosing Test > Live Unit Testing > Start. View the results of the tests within the code editor window as you write and edit code. Click a test result indicator to see more information, such as the names of the tests that cover that method.

Microsoft Visual Studio LightSwitch Unleashed, Unit testing can often take on a rather broad connotation. We try a code, he will write unit tests based on its structure and understanding of the story. It's when you're refactoring, i.e., restructuring a unit's code but without logic methods; Simple DB queries checking predicates; Services that are high-risk  This got me thinking. My workplace had a policy requiring developers to write system tests in addition to having good unit test coverage. Without diving into the testing taxonomy, the term "system tests" refers to tests that are almost end-to-end. They start at the service API level (REST request, in my case) and go all through to the database.

Unit Testing Guidelines: What to Test and What Not to Test, functional programming and test-driven techniques of C# Ovais Mehboob Ahmed Khan, The real logic, if any, for a service exists on the server and doesn​'t need to be tested there is no clear division between our code and their code. any of the tests we will write here as unit tests, we will need to mock the behaviors  Why unit test services? It should be clear already, but just in case: we need to be sure our business logic works correctly. Why unit test repositories? There are several cases to test repositories. Of course we don’t test the framework itself. But we do write unit tests to be sure that our specifications or relations have been implemented

Improving your C# Skills: Solve modern challenges with functional , Build TV input services Your unit tests should nearly exhaust all possible interactions with the The testing development cycle consists of writing a failing unit test, app that typically contains layers representing the UI, business logic, Each module should have only one area of focus, and the APIs that  The goal of this tutorial is to demonstrate how you can write unit tests for the controllers in your ASP.NET MVC applications. We discuss how to build three different types of unit tests. You learn how to test the view returned by a controller action, how to test the View Data returned by a controller action, and how to test whether or not one

Fundamentals of Testing, Most developers put a lot of effort into creating good code structure. This means naming classes and methods consistently, putting them in the right In theory, this should be easy: There are unit tests, integration tests, and end-to-end tests. One time I had to update some web service client logic, I found  I’m not sure we need unit testing any more than we need compilers, or high level languages. Unit testing is just another tool that makes software simpler to write, simpler to change, and simpler to get correct.

Comments
  • If there's nothing to test or the code is trivial such as in simple getters and setters then I'd not write any tests. Writing a test now in case there might be logic added doesn't feel like it makes much sense because you don't know that that logic is going to be. However, depending on your needs, you might still have to test that method to make sure the call chargesRepository.saveAndFlush(charge) succeeds and returns what you need - as I said that would depend on your needs, i.e. do you need to verify that chargesRepository is set or that the charge can be saved (e.g. no missing data)?
  • Well, for these purposes, I can write a test suite for testing all repository methods
  • The general problem of more practical examples is that the requirements and resources differ from team to team, product to product etc. Writing and maintaining tests doesn't come for free so business decisions will probably influence what is tested and how - and explaining those for a more complex example might be a little too much.
  • which similar question? Copying answers without attribution is not permitted.