Spring Boot ਤੁਹਾਨੂੰ ਇੱਕ testing pyramid ਦਿੰਦਾ ਹੈ: ਕਿਸੇ Spring ਤੋਂ ਬਿਨਾਂ ਤੇਜ਼ unit tests, ਕੇਂਦਰਿਤ slice tests ਜੋ context ਦਾ ਹਿੱਸਾ load ਕਰਦੇ ਹਨ, ਅਤੇ ਕੁਝ full-context integration tests। ਹਰ test ਲਈ ਸਹੀ ਪੱਧਰ ਚੁਣਨਾ ਇੱਥੇ senior ਹੁਨਰ ਹੈ।
Unit test — ਬਿਲਕੁਲ ਕੋਈ Spring ਨਹੀਂ
Constructor injection ਦਾ ਮਤਲਬ ਹੈ ਕਿ ਜ਼ਿਆਦਾਤਰ service logic ਨੂੰ ਜ਼ੀਰੋ Spring ਦੀ ਲੋੜ ਹੁੰਦੀ ਹੈ; ਬਸ collaborators ਨੂੰ mock ਕਰੋ:
{
mock(PriceRepository.class);
(repo.base()).thenReturn(BigDecimal.TEN);
(repo);
assertThat(svc.priceOf()).isEqualByComparingTo();
}
