I have a simple lambda that is basically x => "Error: "+x+".". I was just wondering if this was worth a test. It's really just a one-liner, so there's not much that can go wrong.

Should I write a test?

  • While there is the concept that some things are too simple to test, if you have an interpreted language it would be good to at least make sure you don't have any typos. Commented Mar 1, 2022 at 3:15

1 Answer 1


It's about behavior not structure.

Being a lambda isn't the point. Being a one liner isn't the point.

The point is your behavior is well tested string concatenation. Without a requirement I say move along. Nothing interesting to see here.

You can cram an entire operating system into a one liner or a lambda. Stop getting distracted by structure.

  • 1
    That being said though, if the software requirements specify an exact message, testing may be warranted. This matters for integrated components where the value is not merely meant for human readability.
    – Flater
    Commented Mar 1, 2022 at 8:42
  • @Flater true, if the behavior is critical then even if the logic is simple you can justify regression tests, integration tests, and acceptance tests. What you should avoid is testing for no better reason then because you don't trust that string concatenation works. Interesting logic though needs testing even if it doesn't show up in the requirements. Question didn't specify any requirements and this code isn't interesting. Commented Mar 1, 2022 at 16:23
  • I'm not sure about other languages, but in C++ you simply can't unit test a lambda in-situ. You can only unit test a different lambda, because each lambda expression defines a unique class type.
    – Caleth
    Commented Mar 2, 2022 at 10:17
  • @Caleth same is true in Java. What difference does that make? Commented Mar 2, 2022 at 16:42
  • @Flater updated. Better now? Commented Mar 3, 2022 at 20:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.