According to my understanding, true BDD is more about fulfilling business specifications, but I thought starting with class-level behavior might be worthwhile, as I struggled to accept TDD, and BDD assignment made it easier.
Let's focus on unit tests like BDD (at the class level).
I started calling my test classes in a way that reflects expected behavior using "When," as suggested in this article . It is really effective. However, I soon realized that such test class names could have the following maintenance implications.
It’s not easy to find a test class for the class at hand.
I'm used to the traditional naming of "ClassnameTest", in which case I could easily find a test class.
I use Java and JUnit4, and unfortunately I do not have built-in language constructs such as data, etc.
Consider the following example.
public class Lion{...}
Class testing
public class WhenLionIsHungy{
@Test public void should_eat_meat(){...}
@Test public void should_not_eat_grass(){...}
@Test ...
}
public class WhenLionHunts{...}
public class WhenLionIsWithCubs{...}
...
Please forgive me for an example. Suppose all of them are valid test classes. Do not worry about this if you are not sure that we should never have several test classes based on the behavior for the target class.
BDD is an elegant solution for including tests in documentation, and the above example reflects this. Testing a method such as "testMoneyTransfer ()" will never yield any information.
The above example is a happy example, but it would be a nightmare to guess the test class for a class called "LibraryServiceImpl".
IfBookIsRequested WhenLibraryIsClosed - ?
. LibraryServiceImpl .
SRP, -, , , 100 .
@Suite.
@RunWith(Suite.class)
@Suite.SuiteClasses({WhenLionIsHungy.class,
WhenLionHunts.class,
WhenLionIsWithCubs.class
})
public class LionTest{}
?
?
.
@RunWith(Enclosed.class)
public class LionTest{
public static class WhenLionIsHungy{...}
public static class WhenLionHunts{...}
...
}
?
?
, , " ". , .
.
, - .