簡體   English   中英

關於最佳實踐的Spring Boot單元測試和currectli寫作測試

[英]Spring boot Unit test about best practics and currectli writing tests

我想在我的項目中開始編寫單元測試。 我嘗試過多次。 他總是辭職,因為他聽不懂意思。 因為我無法找到知識並將其形成一個整體。 我讀了很多文章,看了很多例子,它們都是不同的。 結果,我了解了為什么需要編寫測試,了解了如何編寫測試,但是我不了解如何正確地編寫測試。 而且我不知道如何編寫它們以便使它們有用。 我有一些疑問:

例如,我有服務:

@Service
public class HumanServiceImpl implements HumanService {

  private final HumanRepository humanRepository;

  @Autowired
  public HumanServiceImpl(HumanRepository humanRepository) {
    this.humanRepository = humanRepository;
  }

  @Override
  public Human getOneHumanById(Long id) {
    return humanRepository.getOne(id);
  }

  @Override
  public Human getOneHumanByName(String firstName) {
    return humanRepository.findOneByFirstName(firstName);
  }

  @Override
  public Human getOneHumanRandom() {
    Human human = new Human();
    human.setId(Long.parseLong(String.valueOf(new Random(100))));
    human.setFirstName("firstName"+ System.currentTimeMillis());
    human.setLastName("LastName"+ System.currentTimeMillis());
    human.setAge(12);//any logic for create Human
    return human;
  }
}

我嘗試為此服務編寫單元測試:

@RunWith(SpringRunner.class)
public class HumanServiceImplTest {

  @MockBean(name="mokHumanRepository")
  private HumanRepository humanRepository;

  @MockBean(name = "mockHumanService")
  private HumanService humanService;

  @Before
  public void setup() {
    Human human = new Human();
    human.setId(1L);
    human.setFirstName("Bill");
    human.setLastName("Gates");
    human.setAge(50);

    when(humanRepository.getOne(1L)).thenReturn(human);
    when(humanRepository.findOneByFirstName("Bill")).thenReturn(human);
  }

  @Test
  public void getOneHumanById() {
    Human found = humanService.getOneHumanById(1L);
    assertThat(found.getFirstName()).isEqualTo("Bill");
  }

  @Test
  public void getOneHumanByName() {
    Human found = humanService.getOneHumanByName("Bill");
    assertThat(found.getFirstName()).isEqualTo("Bill");
  }

  @Test
  public void getOneHumanRandom() {
    //???
  }
}

我有問題:

1.我應該在哪里填充物體? 我看到了不同的實現

@Before就像在我的示例中,在@Test ,混合實現-當Human在@Before創建並表達式時

when(humanRepository.getOne(1L)).thenReturn(human); 

@Test方法中

  private Human human;

  @Before
  public void setup() {
    human = new Human();
    ...
  }

  @Test
  public void getOneHumanById() {
    when(humanRepository.getOne(1L)).thenReturn(human);
    Human found = humanService.getOneHumanById(1L);
    assertThat(found.getFirstName()).isEqualTo("Bill");
  }

2.如何測試getOneHumanRandom()方法?

調用此方法時,服務不使用存儲庫。 我可以使該方法成為模擬方法,但是它將帶來什么呢?

when(humanService.getOneHumanRandom()).thenReturn(human);
...
@Test
  public void getOneHumanRandom() {
    Human found = humanService.getOneHumanRandom();
    assertThat(found.getFirstName()).isEqualTo("Bill");
  }

我只是從測試類的服務中復制邏輯。 這種測試的目的是什么,有必要嗎?

1.我應該在哪里填充物體? 我看到了不同的實現

我將在所有/大多數測試之間使用@Before進行任何常見設置。 任何特定於特定測試的設置都應納入該測試方法。 如果某些(而非全部)測試之間存在通用設置,則可以編寫專用設置方法。

記住要保持測試/代碼干燥(不要重復!)。 測試有一個維護因素,將通用代碼保持在一起,有助於減輕將來的麻煩。

2.如何測試getOneHumanRandom()方法?

您應該創建一個Human toString()方法。 此方法應合並Human對象上的所有屬性。 您可以調用getOneHumanRandom()兩次,並斷言它們不相等。

@Test
public void getOneHumanRandomTest()
{
    // SETUP / TEST
    Human one = service.getOneHumanRandom();
    Human two = service.getOneHumanRandom();
    // VERIFY / ASSERT
    assertNotEquals("these two objects should not be equal", one.toString(), two.toString())
}

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM