Behavior Driven Development - jamongx/twitter-clone GitHub Wiki

Focus:

  • Behavioral-Driven Development (BDD) is a testing approach derived from the Test-Driven Development (TDD) methodology.
  • BDD is designed to allow not only developers but also non-developers (such as business analysts and product owners) to participate.
  • This methodology focuses on the behavior of software, i.e., user requirements or system responses.

Process:

  • In BDD, tests are written based on user stories and scenarios.
  • These tests are written in language that is easier for people to understand and describe the behavior of the system.

Testing Units:

  • Unit Testing: Controller Layer, Service Layer, Repository Layer
  • Integration Testing: Based on Controller Layer

Tools:

  • BDD frameworks such as Cucumber, SpecFlow, and JBehave are used.

BDD Example

  • User Management Feature (UserController, UserService, and UserRepository)
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    private UserRepository userRepository;

    @Override
    public UserDto getUser(Long id) {
        ...
    }
}
  • BDD TEST CODE
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.BDDMockito.*;

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

    @Mock
    UserRepository userRepository;

	@InjectMocks  
	private UserServiceImpl userService;

    @Test
    void shouldReturnUserWhenUserIdIsProvided() {
        // given
        given(userRepository.getUser("username")).willReturn(new User("username"));

        // when
        User result = userService.getUser("username");

        // then
        assertEquals("username", result.getId());
    }
}