POJO的单元测试使用Mockito

huangapple go评论67阅读模式
英文:

Unit test for POJO using Mockito

问题

我有一个包含另一个POJO变量的POJO类

```java
class Rows 
{
    private List<RowName> rowName; //RowName是另一个具有String变量的POJO
    //getter和setter
}

测试案例通过了,但我不确定我是否在正确地进行测试。

public class RowsTest
{
    private final List<RowName> rowName = Mockito.mock(List.class);
    
    private final Rows target = new Rows(rowName);

    @Test
    public void testGetMethods()
    {
        //然后
        Assert.assertEquals(rowName, target.getRowName());
    }
}
英文:

I have a POJO class with another POJO variable

class Rows 
{
    private List&lt;RowName&gt; rowName; //RowName is another POJO with String variables
    //getter &amp; setter
}

The test case passes, but I am not sure I am testing this correctly.

public class RowsTest
{
    private final List&lt;RowName&gt; rowName = Mockito.mock(List.class);
    
    private final Rows target = new Rows(rowName);

    @Test
    public void testGetMethods()
    {
        //then
        Assert.assertEquals(rowName, target.getRowName());
    }
}

</details>


# 答案1
**得分**: 0

在编写模拟测试时您需要考虑:“我要锁定哪些功能以便它们在将来不会意外更改。”

仅测试数据类可以确保 Java 在将来不会破坏它而 Java 已经在其发布周期中对其进行了测试适用于下个版本

测试您的程序中与您的自定义逻辑有关的部分但不包括您使用的库和语言

<details>
<summary>英文:</summary>

When writing Mock tests you need to think &quot;What functionality am I locking in so it cannot unexpectedly change in the future&quot;

Testing data classes only ensures Java doesn&#39;t break it in the future, and Java has tests for that in their release cycle on the next versions.

Test logic you have which is custom to your program, but not the libraries and languages you use.

</details>



huangapple
  • 本文由 发表于 2020年9月25日 02:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64052739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定