所以我开始为我们的 Java-Spring-project 编写测试。
我使用的是 JUnit 和 Mockito。据说,当我使用 when()...thenReturn() 选项时,我可以模拟服务,而无需模拟它们左右。所以我想做的是,设置:
when(classIwantToTest.object.get().methodWhichReturnsAList(input))thenReturn(ListcreatedInsideTheTestClass)
但无论我执行哪个 when 子句,我总是会得到一个 NullpointerException,这当然是有道理的,因为输入为空。
此外,当我尝试从对象模拟另一种方法时:
when(object.method()).thenReturn(true)
在那里我还得到了一个 Nullpointer,因为该方法需要一个未设置的变量。
但我想使用 when()..thenReturn() 来绕过创建这个变量等等。我只是想确保,如果任何类调用此方法,那么无论如何,只返回 true 或上面的列表。
这基本上是我的误解,还是有其他问题?
代码:
public class classIWantToTest implements classIWantToTestFacade{
@Autowired
private SomeService myService;
@Override
public Optional<OutputData> getInformations(final InputData inputData) {
final Optional<OutputData> data = myService.getListWithData(inputData);
if (data.isPresent()) {
final List<ItemData> allData = data.get().getItemDatas();
//do something with the data and allData
return data;
}
return Optional.absent();
}
}
这是我的测试课:
public class Test {
private InputData inputdata;
private ClassUnderTest classUnderTest;
final List<ItemData> allData = new ArrayList<ItemData>();
@Mock
private DeliveryItemData item1;
@Mock
private DeliveryItemData item2;
@Mock
private SomeService myService;
@Before
public void setUp() throws Exception {
classUnderTest = new ClassUnderTest();
myService = mock(myService.class);
classUnderTest.setService(myService);
item1 = mock(DeliveryItemData.class);
item2 = mock(DeliveryItemData.class);
}
@Test
public void test_sort() {
createData();
when(myService.getListWithData(inputdata).get().getItemDatas());
when(item1.hasSomething()).thenReturn(true);
when(item2.hasSomething()).thenReturn(false);
}
public void createData() {
item1.setSomeValue("val");
item2.setSomeOtherValue("test");
item2.setSomeValue("val");
item2.setSomeOtherValue("value");
allData.add(item1);
allData.add(item2);
}
classIwantToTest.object.get()
结果不是模拟,或者 get()
返回 null。但是请发布您的代码,显示您的测试和模拟的初始化。
我遇到了这个问题,我的问题是我使用 any()
而不是 anyInt()
调用我的方法。所以我有:
doAnswer(...).with(myMockObject).thisFuncTakesAnInt(any())
我不得不将其更改为:
doAnswer(...).with(myMockObject).thisFuncTakesAnInt(anyInt())
我不知道为什么会产生 NullPointerException。也许这会帮助下一个可怜的灵魂。
对于布尔方法,尚未存根的方法的默认返回值为 false
,对于返回集合或映射的方法为空集合或映射,否则为 null
。
这也适用于 when(...)
中的方法调用。在您的示例中,when(myService.getListWithData(inputData).get())
将导致 NullPointerException,因为 myService.getListWithData(inputData)
是 null
- 它之前没有被存根。
一种选择是为所有中间返回值创建模拟,并在使用前存根它们。例如:
ListWithData listWithData = mock(ListWithData.class);
when(listWithData.get()).thenReturn(item1);
when(myService.getListWithData()).thenReturn(listWithData);
或者,您可以在创建模拟时指定不同的默认答案,以使方法返回新模拟而不是 null:RETURNS_DEEP_STUBS
SomeService myService = mock(SomeService.class, Mockito.RETURNS_DEEP_STUBS);
when(myService.getListWithData().get()).thenReturn(item1);
您应该阅读 Mockito.RETURNS_DEEP_STUBS 的 Javadoc,其中更详细地解释了这一点,并且还对其用法有一些警告。
我希望这有帮助。请注意,您的示例代码似乎有更多问题,例如缺少断言或验证语句以及在模拟上调用设置器(这没有任何效果)。
我遇到了同样的问题,我的问题只是我没有使用 @RunWith 正确注释类。在您的示例中,请确保您拥有:
@RunWith(MockitoJUnitRunner.class)
public class Test {
...
一旦我这样做了,NullPointerExceptions 就消失了。
对于未来的读者,使用模拟时 NPE 的另一个原因是忘记初始化模拟,如下所示:
@Mock
SomeMock someMock;
@InjectMocks
SomeService someService;
@Before
public void setup(){
MockitoAnnotations.initMocks(this); //without this you will get NPE
}
@Test
public void someTest(){
Mockito.when(someMock.someMethod()).thenReturn("some result");
// ...
}
还要确保所有注释都使用 JUnit。我曾经不小心用来自 testNG 的 @Test 创建了一个测试,所以 @Before 不能使用它(在 testNG 中,注释是 @BeforeTest)
org.junit.jupiter.api.Test
而不是 import org.junit.Test
这导致模拟显然为空。
对我来说,我获得 NPE 的原因是我在模拟原语时使用了 Mockito.any()
。我发现通过切换到使用来自 mockito 的正确变体可以消除错误。
例如,要模拟一个将原始 long
作为参数的函数,而不是使用 any()
,您应该更具体,并将其替换为 any(Long.class)
或 Mockito.anyLong()
。
希望对某人有所帮助。
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced or misused argument matcher detected... You cannot use argument matchers outside of verification or stubbing.
由于这是我发现的与我遇到的问题最接近的结果,因此这是出现的第一个结果,但我没有找到合适的答案,我将在此处为任何未来的可怜人发布解决方案:
any()
在模拟类方法使用原始参数的情况下不起作用。
public Boolean getResult(String identifier, boolean switch)
以上将产生与OP相同的问题。
解决方案,只需包装它:
public Boolean getResult(String identifier, Boolean switch)
后者解决了NPE。
请记住,如果您选择这种方法,现在您可能希望在生产代码中包含布尔值的空检查(信用:由 Ridcully 提出)
确保你初始化你的模拟。
JUnit4
使用 @Before
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
JUnit5
使用 @BeforeEach
@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
}
对于 JUnit5
检查,您也在使用正确的导入。
import org.junit.runner.RunWith
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
特殊情况:
如果您使用 Scala 并尝试在 值类 上创建 any
匹配器,则会得到一个无用的 NPE。
所以给定 case class ValueClass(value: Int) extends AnyVal
,你想要做的是 ValueClass(anyInt)
而不是 any[ValueClass]
when(mock.someMethod(ValueClass(anyInt))).thenAnswer {
...
val v = ValueClass(invocation.getArguments()(0).asInstanceOf[Int])
...
}
另一个 SO question 更具体地说明了这一点,但如果您不知道问题出在值类上,您就会错过它。
您需要初始化 MockitoAnnotations.initMocks(this) 方法必须调用以初始化带注释的字段。
@Before public void initMocks() {
MockitoAnnotations.initMocks(this);
}
有关详细信息,请参阅 Doc
org.junit.jupiter.api.BeforeEach
insted of @Before
帮助了我。
对于 JUnit 5,测试类必须使用以下注释:
@ExtendWith(MockitoExtension.class)
进口:
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
我的问题已通过此添加解决。
另一个常见的问题是方法签名被意外声明为“final”。
这吸引了很多从事代码库工作的人,这些代码库受到 Checkstyle 的影响,并内化了将成员标记为 final
的需求。
即在OP的例子中:
object.method()
确保 method()
未声明为 final
:
public final Object method() {
}
Mockito 无法模拟最终方法,这将作为一个包装的 NPE 出现:
Suppressed: org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
深埋在错误消息中的是以下内容:
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
对我来说,这是因为我在 @BeforeAll
方法中对模拟进行了存根。
MockitoExtension
没有 @BeforeAll
的回调。
public class MockitoExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver
我在它工作的测试方法中移动了存根!
就我而言,Intellij 使用 org.junit.jupiter.api.Test
(Junit5) 而不是 import org.junit.Test
of (Junit4) 创建了 Test,这导致所有 bean 显然都为空。另外,确保类和测试方法是公开的
就我而言,我错过了 add first
PowerMockito.spy(ClassWhichNeedToBeStaticMocked.class);
所以这对看到此类错误的人会有所帮助
java.lang.NullPointerException
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:67)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:42)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:112)
@RunWith(MockitoJUnitRunner.class) //(OR) PowerMockRunner.class
@PrepareForTest({UpdateUtil.class,Log.class,SharedPreferences.class,SharedPreferences.Editor.class})
public class InstallationTest extends TestCase{
@Mock
Context mockContext;
@Mock
SharedPreferences mSharedPreferences;
@Mock
SharedPreferences.Editor mSharedPreferenceEdtor;
@Before
public void setUp() throws Exception
{
// mockContext = Mockito.mock(Context.class);
// mSharedPreferences = Mockito.mock(SharedPreferences.class);
// mSharedPreferenceEdtor = Mockito.mock(SharedPreferences.Editor.class);
when(mockContext.getSharedPreferences(Mockito.anyString(),Mockito.anyInt())).thenReturn(mSharedPreferences);
when(mSharedPreferences.edit()).thenReturn(mSharedPreferenceEdtor);
when(mSharedPreferenceEdtor.remove(Mockito.anyString())).thenReturn(mSharedPreferenceEdtor);
when(mSharedPreferenceEdtor.putString(Mockito.anyString(),Mockito.anyString())).thenReturn(mSharedPreferenceEdtor);
}
@Test
public void deletePreferencesTest() throws Exception {
}
}
以上所有注释代码都不需要{mockContext = Mockito.mock(Context.class);
},如果你使用@Mock注解到Context mockContext;
@Mock
Context mockContext;
但是,如果您仅使用 @RunWith(MockitoJUnitRunner.class) 它将起作用。根据 Mockito,您可以使用 @Mock 或 Mockito.mock(Context.class); 创建模拟对象。 ,
由于使用了@RunWith(PowerMockRunner.class),我得到了 NullpointerException,而不是我改为@RunWith(MockitoJUnitRunner.class) 它工作正常
以上答案都没有帮助我。我一直在努力理解为什么代码在 Java 中有效,但在 Kotlin 中无效。
然后我从 this thread 中弄清楚了。
您必须创建类和成员函数 open
,否则会抛出 NPE。
制作函数 open
后,测试开始通过。
您不妨考虑使用编译器的 "all-open" plugin:
Kotlin 默认具有类及其成员 final,这使得使用 Spring AOP 等要求类开放的框架和库很不方便。全开放编译器插件使 Kotlin 适应这些框架的要求,并使带有特定注释的类及其成员在没有显式 open 关键字的情况下打开。
那么在我的情况下,这是因为错误的注释使用。我使用 junit 4 进行测试并在初始化时使用 @BeforeEach
而不是 @Before
。
将其更改为 @Before
,它就像魅力一样。
就我而言,这是对 when()
的错误导入。
我偶然使用了 import static reactor.core.publisher.Mono.when
。
Ed Webb 的 answer 对我有帮助。相反,您也可以尝试添加
@Rule public Mocks mocks = new Mocks(this);
如果您@RunWith(JUnit4.class)
。
这些答案都不适合我。这个答案并不能解决 OP 的问题,但由于这篇文章是唯一出现在谷歌搜索这个问题的帖子,所以我在这里分享我的答案。
我在为 Android 编写单元测试时遇到了这个问题。问题是我正在测试的活动扩展了 AppCompatActivity
而不是 Activity
。为了解决这个问题,我可以将 AppCompatActivity
替换为 Activity
,因为我真的不需要它。这对每个人来说可能不是一个可行的解决方案,但希望知道根本原因会对某人有所帮助。
使用 JUnit 5 或更高版本时。您必须在 @BeforeEach
设置中注入带有 @Mock
注释的类。
在我的情况下,这是由于 @Test 注释的错误导入
确保您使用的是以下导入
import org.junit.jupiter.api.Test;
使用 @ExtendWith(MockitoExtension.class)
注释测试类。
当我与 Junit 5 有相同的 NullPointerException
,但在我的 maven 项目中正确使用 @ExtendWith(MockitoExtension.class)
时,这是 google 带我去的地方。
原来我没有在我的 pom.xml 中包含 maven-surefire-plugin
,这意味着 @ExtendWith
实际上没有做任何事情!
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
...
在我的例子中,一个测试方法调用另一个方法作为参数:
Mockito.`when`(repository.getItems(prefs.getUser().id)).thenReturn(listOf())`
当 repository
被模拟时,prefs.getUser().id)
将抛出 NPE。所以,首先我们应该模拟一个参数,例如,
Mockito.`when`(prefs.getUser()).thenReturn(User(id = 1, name = "user"))`
我们还应该模拟 prefs
。我没有检查它并改变了一个图书馆,对不起。
我试图模拟一个“最终”方法,这显然是问题所在。
处理此问题的正确方法是使用接口并模拟该接口,但是我无法控制“最终”方法所在的库。
Mockito 2 可以处理模拟 final 方法。在项目的 src/test/resources/mockito-extensions 目录中添加一个名为 org.mockito.plugins.MockMaker 的文本文件并添加一行文本:
mock-maker-inline
之后,模拟最终方法应该可以正常工作。
检查您使用的 Junit 版本。在 Maven/Gradle 构建工具中,如果你设置使用 testRuntimeOnly 'junit5'
,那么它可能不需要 @RunWith
,因为它不可用,在 Junit5 中它被替换为 @ExtendWith
。
就我而言,我在映射器声明中缺少注释 @mock
。
any()
适用于所有变量并将其表现为 NPE 无异于对开发团队的网络战攻击。