小编sur*_*ana的帖子

Powermockito可以在非最终具体类中模拟最终方法吗?

假设我有一个非最终的具体类,其最终方法如下所示.

public class ABC {
  public final String myMethod(){
      return "test test";
  }
}
Run Code Online (Sandbox Code Playgroud)

是否有可能myMethod()junit使用时调用返回其他东西Powermockito?谢谢

java unit-testing powermock

12
推荐指数
1
解决办法
2万
查看次数

MockitoInvocationHandler类的NoClassDefFoundError

我正在使用mockito-all-1.9.5-rc1.jarpowermock-mockito-1.4.12-full.jar.当我在非final类中运行这个简单的单元测试来模拟final方法时.

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ABC.class)
public class ABCTest {

    @Test
    public void finalCouldBeMock() {
        final ABC abc = PowerMockito.mock(ABC.class);
        PowerMockito.when(abc.myMethod()).thenReturn("toto");
        assertEquals("toto", abc.myMethod());
    }

}
Run Code Online (Sandbox Code Playgroud)

当我跑的时候,我得到了 java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler Caused by: java.lang.ClassNotFoundException: org.mockito.internal.MockitoInvocationHandler

当我搜索FO类MockitoInvocationHandlermockito-all-1.9.5-rc1.jarpowermock-mockito-1.4.12-full.jar.我找不到任何东西.需要帮助解决这个问题!谢谢

java junit4 mockito powermock

7
推荐指数
1
解决办法
1万
查看次数

如何使用Spring和Hibernate在项目中通过PowerMockito模拟SessionFactory或Session?

我正在开发一个使用Spring和Hibernate的项目.我有一个包含SessionFactory的抽象DAO.我需要为我的单元测试模拟SessionFactory或Session.但到目前为止我还没有成功.下面是代码.任何人有任何想法?谢谢.

这是我的AbstractDAO

public abstract class AbstractDAO {
    protected JdbcTemplate jdbcTemplate;
    protected SessionFactory sessionFactory;
    protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    ...

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
    }}
Run Code Online (Sandbox Code Playgroud)

这是我的ConcreteDAO

public class LoginDAO extends AbstractDAO implements InitializingBean {
    ...
    public User getLoggedinUserByUserid(Long userid){
            log.info("in getLoggedinUserByUserid"); 
            User result = null;
            Session session = sessionFactory.openSession();
            try {
            session.beginTransaction();
            result = (User) session.get(User.class, userid);
            session.getTransaction().rollback();
            session.close();
            } catch (Exception e) {
            log.error(e,e);
            session.getTransaction().rollback();
            session.close();
            }

    return result;
     }
...}
Run Code Online (Sandbox Code Playgroud)

这是我的测试课

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "LoginDAOTest-context.xml" …
Run Code Online (Sandbox Code Playgroud)

junit spring hibernate mockito powermock

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

powermock ×3

java ×2

mockito ×2

hibernate ×1

junit ×1

junit4 ×1

spring ×1

unit-testing ×1