2

I am trying to learn Propery-Based Testing(PBT)I think I know how to implement it but when should I apply PBT?

For example in this case I am trying to compare if the function getCurrentName() returns the expected name. Should I randomize this test?

@Test
public void getNameTest() {
    assertEquals(nameProxy, proxyFoto.getCurrentName());
}
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
jorge.munin
  • 27
  • 1
  • 5
  • What are `nameProxy` and `proxyFoto`? – Mark Seemann Dec 13 '21 at 11:09
  • `nameProxy` is a String with the value "name" and `proxyFoto` is an object that has a `long id; a String name; Date date;` getCurrentName just return the name – jorge.munin Dec 13 '21 at 11:40
  • Are you asking whether you should test a *property getter?* [You can](https://blog.ploeh.dk/2013/03/08/test-trivial-code), but it's quite [contentious whether you should](https://blog.ploeh.dk/2018/11/12/what-to-test-and-not-to-test) - even before we start to consider example-based testing versus property-based testing. – Mark Seemann Dec 13 '21 at 17:07

1 Answers1

1

Your question is so generic that it cannot have a specific answer. I suggest you look at some of the stuff that has been written about how to come up with good properties, e.g. https://johanneslink.net/how-to-specify-it/

As for your concrete example, the answer if writing a property for the current name makes sense depends on a few things:

  • How does the name get into the proxy object? Is there a reasonable chance that depending on the shape/length/encoding etc of the name the behaviour is different?
  • What is the name being used for? Should it be normalised, formatted, shortened or processed in any specific way?

Properties and PBT are about finding and falsifying assumptions about the behaviour of your code under test. If there is nothing that you might get wrong, any form of automated testing can be considered unnecessary. If there are quite a few edge cases and paths that could show unexpected behaviour, then PBT looks like a worthwhile approach.

As a pragmatic recommendation: Start to translate some of your example tests into properties and see which ones are pulling their weight. Then try to add additional properties, eg by using ideas from the article I linked to.

johanneslink
  • 4,877
  • 1
  • 20
  • 37