我已经阅读过各种关于模拟和测试中存根的文章,包括Martin Fowler的Mocks Are Not Stubs,但仍然不明白其中的区别.
我写了一个工厂来生产java.sql.Connection物品:
public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory {
@Override public Connection getConnection() {
try {
return DriverManager.getConnection(...);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想验证传递给的参数DriverManager.getConnection,但我不知道如何模拟静态方法.我正在使用JUnit 4和Mockito来测试我的测试用例.有没有一种很好的方法来模拟/验证这个特定的用例?
我正在使用Mockito 1.9.0.我想在JUnit测试中模拟一个类的单个方法的行为,所以我有
final MyClass myClassSpy = Mockito.spy(myInstance);
Mockito.when(myClassSpy.method1()).thenReturn(myResults);
Run Code Online (Sandbox Code Playgroud)
问题是,在第二行,myClassSpy.method1()实际上是被调用,导致异常.我使用模拟的唯一原因是,以后,无论什么时候myClassSpy.method1()被调用,都不会调用真正的方法并myResults返回对象.
MyClass是一个接口myInstance,如果重要的话就是它的实现.
我需要做些什么来纠正这种间谍行为?
我在我的类中使用私有静态最终LOGGER字段,我希望LOGGER.isInfoEnabled()方法返回false.如何使用mockito或jMockit模拟静态final字段
我的班级是:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Class1 {
private static final Logger LOGGER = LoggerFactory.getLogger(Class1.class);
public boolean demoMethod() {
System.out.println("Demo started");
if (LOGGER.isInfoEnabled()) {
System.out.println("@@@@@@@@@@@@@@ ------- info is enabled");
} else {
System.out.println("info is disabled");
}
return LOGGER.isInfoEnabled();
}
}
Run Code Online (Sandbox Code Playgroud)
它的junit是:
import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import com.source.Class1;
public class MyTest {
@InjectMocks
Class1 cls1;
@BeforeMethod
public void initMocks() { …Run Code Online (Sandbox Code Playgroud) 我想用mockito测试我的servlet.我也想知道服务器输出是什么.所以如果servlet写出这样的东西:
HttpServletResponse.getWriter().println("xyz");
Run Code Online (Sandbox Code Playgroud)
我想把它写成文本文件.我为HttpServletResponse创建了模拟,并告诉Mockito如果调用了HttpServletResponse.getWriter(),它应该返回我的自定义PrintWriter:
HttpServletResponse resp = mock(HttpServletResponse.class);
PrintWriter writer = new PrintWriter("somefile.txt");
when(resp.getWriter()).thenReturn(writer);
Run Code Online (Sandbox Code Playgroud)
生成文本文件,但它是空的.我怎样才能使这个工作?
编辑:
@Jonathan:这实际上是真的,嘲笑作家也是一个更清洁的解决方案.解决了这个问题
StringWriter sw = new StringWriter();
PrintWriter pw =new PrintWriter(sw);
when(resp.getWriter()).thenReturn(pw);
Run Code Online (Sandbox Code Playgroud)
然后我可以检查StringWriter的内容,而不必处理文件.
我想在 Mockito 中模拟一个静态方法。
据我所知这是不可能的,我该如何解决这个问题?powermock 不是一个选项。
我希望我的身份验证变量不会为空。
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Run Code Online (Sandbox Code Playgroud)
我想在junit5中模拟静态方法。但不幸的是,Junit5不支持Powermockito。除了恢复到Junit4之外,还有其他方法可以实现相同的目的吗
我正在测试我的弹簧控制器并尝试执行以下操作
调节器
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
String role = String.valueOf(authentication.getAuthorities());
if(role.contains("user")) {
...
}
Run Code Online (Sandbox Code Playgroud)
测试
@Test
public void testLoginUser() throws Exception {
User user = new User();
user.setLogin("user");
user.setRoleid(1L);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user,user));
when(String.valueOf(Collection.class)).thenReturn("user");
Run Code Online (Sandbox Code Playgroud)
但我明白了
org.mockito.exceptions.misusing.MissingMethodInvocationException
Run Code Online (Sandbox Code Playgroud)
我需要我的if块才能true执行.
有什么办法可以做到吗?
我有一个有多个静态方法的类.1静态方法调用另一个私有方法,最终调用第二个公共静态方法.我想模仿第二个静态方法.那可能吗.例如
public static A(){
b();
}
private static b(){
c();
}
public static c(){
}
Run Code Online (Sandbox Code Playgroud)
我想模拟c(),但希望保持a()和b()的功能.这可能吗?如果是这样,怎么样?