【问题标题】:@Before function is not fire in Junit test case@Before 函数在 Junit 测试用例中未触发
【发布时间】:2018-04-09 06:40:36
【问题描述】:

这里我使用 Junit 和 Mockito 编写了一个简单的测试用例。

import org.jbehave.core.annotations.Given;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import com.test.dao.login.LoginDao;
import com.test.mapping.user.User;
import com.test.service.login.LoginService;
import com.test.service.login.impl.LoginServiceImpl;
import com.test.util.common.Common;

public class UserLoginSteps {
    @Mock
    Common common;

    @Mock
    LoginDao loginDao;

    @InjectMocks
    LoginService loginService =new LoginServiceImpl();

    @BeforeClass
    public static void beforeClass() {
        System.out.println("@BeforeClass");
    }

    @Before
    public void before() {
        System.out.println("@Before");
        MockitoAnnotations.initMocks(this);
    }

    @After
    public void after() {
        System.out.println("@After");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("@AfterClass");
    }


    @Given("$username username and $password password")
    @Test
    public void checkUser(String username, String password) throws Exception{

        when(common.checkNullAndEmpty("admin")).thenReturn(true);
        when(common.checkNullAndEmpty("password")).thenReturn(true);
        when(loginDao.getUser("admin","password")).thenReturn(new User());

        assertEquals(true,loginService.checkValidUser(username, password));
    }
}

我已经在 before() 函数中初始化了 Mock 对象。 但是在运行测试用例时不会触发该功能。

我正在使用以下依赖项。

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.8.9</version>
    <scope>test</scope>
</dependency>

我已经看到与这种情况类似的问题。 但以下建议并不能解决问题。

是否有人可以描述它发生的原因以及如何解决此问题,这将非常有帮助。 提前致谢。

After-before-not-working-in-testcase

Simple-junit-class-is-not-calling-the-before-method

Why-isnt-my-beforeclass-method-running

【问题讨论】:

    标签: java unit-testing junit mockito


    【解决方案1】:

    你应该用 @RunWith(MockitoJUnitRunner.class) 注释你的类,所以 MickitoJunitRunner 会处理你的 Mocks 和测试。但它不会像这样与 JBehave 一起工作。您必须决定是要使用 JBehave 还是 MockitoJUnitRunner。

    在 JBehave 中要使用的正确注释是:@BeforeScenario@AfterScenario@BeforeStory@AfterStory 请查看 jbehave 文档:http://jbehave.org/reference/stable/annotations.html

    【讨论】:

    • 您也可以在@Before init(); 方法中使用Mockito.initMocks(this);。 - 不应该出现在@BeforeClass!
    • (BeforeScenario AfterScenario BeforeStory AfterStory) 这就是我真正想要的。谢谢@ArturB3
    猜你喜欢
    • 2013-01-21
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多