使用Mockito/JUnit-Database操作进行Java应用程序的单元测试 [英] Unit testing Java application with Mockito/JUnit-Database operations

查看:237
本文介绍了使用Mockito/JUnit-Database操作进行Java应用程序的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是实现JUnit测试的初学者.我有一个如下所示的测试用例.我也尝试了when ... thenturn + verify方法.

@RunWith(MockitoJUnitRunner.class)
public class CategoryControllerTest {

    @InjectMocks
    CategoryController categeoryCntrlr;
    @Mock
    private CategoryService catSvc;
    //private CategoryController catCntrl;

    @Test
    public void insertCategoryTest(){
        Category cat=new Category();
        cat.setStrCatName("Vehicle");
        String str = categeoryCntrlr.insertCategory(cat);
        System.out.println(str);
        assertEquals("failure",str);
    }
}

我的控制器实际上返回了字符串成功",但我的上述测试用例始终将str的值表示为null.

当我使用when ... thenreturn()进行验证时,即使我将返回值更改为测试用例通过的其他值,它实际上也不在测试我的存储库方法.我知道这里有行为和测试.有什么办法可以让我们俩在一起?我可以得到一些初学者教程(我看过计算器示例并找到经过测试的方法,但没有插入/更新数据库)吗?我无法理解Mockito的参考文档以及它的实际测试方式.有人可以帮我吗?

解决方案

我不确定100%确定insertCategoryTest()的目标是什么,但是您可能想在控制器和/或服务中添加方法.

例如,要在控制器中对insertCategory()方法进行存根以始终返回"failure",请执行以下操作:

when(categeoryCntrlr.insertCategory(any())).thenReturn("failure");

这里有更多有关验证交互和存根方法调用的示例: http://mockito.org

链接到Javadoc:

http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html

I am just beginner for implementing JUnit testing. I have a test case as given below. I have tried the when...thenreturn + verify method as well.

@RunWith(MockitoJUnitRunner.class)
public class CategoryControllerTest {

    @InjectMocks
    CategoryController categeoryCntrlr;
    @Mock
    private CategoryService catSvc;
    //private CategoryController catCntrl;

    @Test
    public void insertCategoryTest(){
        Category cat=new Category();
        cat.setStrCatName("Vehicle");
        String str = categeoryCntrlr.insertCategory(cat);
        System.out.println(str);
        assertEquals("failure",str);
    }
}

My Controller returns actually string 'success' but my above test case always says value of str as null.

When I used when...thenreturn() and verify, it is not actually testing my repository method, even if I change the return to something else the test case is passing. I know there is behavioral and testing. Is there any solution we can have both together? Can I get some beginners tutorial(I saw the calculator example and find methods tested, but no insert/update database)? I am not able to understand the reference document of Mockito, how it actually testing. Can anyone help me please ?

解决方案

I'm not 100% sure what your goals are for insertCategoryTest(), but you probably want to stub methods in your controller and/or service.

For example, to stub the insertCategory() method in your controller to always return "failure", you'd do the following:

when(categeoryCntrlr.insertCategory(any())).thenReturn("failure");

There are more examples about verifying interactions and stubbing method calls here: http://mockito.org

Link to Javadoc:

http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html

这篇关于使用Mockito/JUnit-Database操作进行Java应用程序的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆