我想将一个Mockito模拟对象注入一个Spring(3+)bean中,以便使用JUnit进行单元测试.我的bean依赖项目前通过@Autowired在私有成员字段上使用注释来注入.
我考虑过使用ReflectionTestUtils.setField但我希望注入的bean实例实际上是一个代理,因此不会声明目标类的私有成员字段.我不希望为依赖创建一个公共setter,因为我将修改我的界面纯粹是为了测试的目的.
我遵循了Spring社区给出的一些建议,但是没有创建模拟并且自动连线失败:
<bean id="dao" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.package.Dao" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我目前遇到的错误如下:
...
Caused by: org...NoSuchBeanDefinitionException:
No matching bean of type [com.package.Dao] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {
@org...Autowired(required=true),
@org...Qualifier(value=dao)
}
at org...DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(D...y.java:901)
at org...DefaultListableBeanFactory.doResolveDependency(D...y.java:770)
Run Code Online (Sandbox Code Playgroud)
如果我将constructor-arg值设置为无效,则在启动应用程序上下文时不会发生错误.
有没有办法使用mockitos ArgumentCaptore捕获特定类型的列表.这不起作用:
ArgumentCaptor<ArrayList<SomeType>> argument = ArgumentCaptor.forClass(ArrayList.class);
Run Code Online (Sandbox Code Playgroud) 我想通过mockito验证验证方法是否被调用两次或多次.我使用验证,它抱怨像这样:
org.mockito.exceptions.verification.TooManyActualInvocations:
Wanted 1 time:
But was 2 times. Undesired invocation:
Run Code Online (Sandbox Code Playgroud) JUnit框架包含2个Assert类(显然在不同的包中),并且每个类的方法看起来非常相似.任何人都可以解释为什么会这样吗?
我所指的课程是:junit.framework.Assert和org.junit.Assert.
我知道我可以从src/test/resources加载一个文件:
getClass().getResource("somefile").getFile()
Run Code Online (Sandbox Code Playgroud)
但是我如何获得src/test/resources 目录的完整路径,即我不想加载文件,我只是想知道目录的路径?
在JUnit 3中,我可以得到当前运行的测试的名称,如下所示:
public class MyTest extends TestCase
{
public void testSomething()
{
System.out.println("Current test is " + getName());
...
}
}
Run Code Online (Sandbox Code Playgroud)
这将打印"当前测试是testSomething".
在JUnit 4中是否有任何开箱即用或简单的方法可以做到这一点?
背景:显然,我不想只打印测试的名称.我想加载存储在与测试同名的资源中的特定于测试的数据.你知道,约定优于配置和所有这些.
我正在寻找一个JSON解析库,它支持比较忽略子命令的两个JSON对象,特别是用于测试从Web服务返回的JSON的单元测试.
任何主要的JSON库都支持这个吗?org.json库只是进行参考比较.
我正在使用JUnit-dep 4.10和Hamcrest 1.3.RC2.
我创建了一个自定义匹配器,如下所示:
public static class MyMatcher extends TypeSafeMatcher<String> {
@Override
protected boolean matchesSafely(String s) {
/* implementation */
}
@Override
public void describeTo(Description description) {
/* implementation */
}
@Override
protected void describeMismatchSafely(String item, Description mismatchDescription) {
/* implementation */
}
}
Run Code Online (Sandbox Code Playgroud)
使用Ant从命令行运行时,它完全正常.但是当从IntelliJ运行时,它失败了:
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at com.netflix.build.MyTest.testmyStuff(MyTest.java:40)
Run Code Online (Sandbox Code Playgroud)
我的猜测是它使用了错误的hamcrest.MatcherAssert.我如何找到它使用的hamcrest.MatcherAssert(即哪个jar文件用于hamcrest.MatcherAssert)?AFAICT,我班级路径中唯一的火腿罐是1.3.RC2.
IntelliJ IDEA使用它自己的JUnit或Hamcrest副本吗?
如何输出IntelliJ正在使用的运行时CLASSPATH?
对于我正在使用的仪器测试
@RunWith(AndroidJUnit4.class)
Run Code Online (Sandbox Code Playgroud)
从
import androidx.test.runner.AndroidJUnit4;
Run Code Online (Sandbox Code Playgroud)
为了建立我的测试用例.现在,这条线被标记为与提示使用过时AndroidJUnit4的
import androidx.test.ext.junit.runners.AndroidJUnit4
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试AndroidJUnit4从命名包导入我得到错误,ext无法解决.
您是否有任何想法,应该在gradle中包含哪些包来解决此问题?
我知道一种方法是:
@Test
public void foo(){
try{
//execute code that you expect not to throw Exceptions.
}
catch(Exception e){
fail("Should not have thrown any exception");
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更清洁的方法来做到这一点.(可能使用Junit的@Rule?)