Jmockit:无法模拟内部领域。总是以 null 结束
Jmockit: Unable to mock inner field. Always ends up being null
我无法控制我想测试的方法中的变量发生了什么。在此示例中,被测试方法的输入是模拟对象或可注入对象,但如果您尝试对其执行任何操作,则会出现 Null* 异常。
Class及正在测试的方法:
public class SomeClass {
public SomeClass() {}
public void myMethod(InputClass input) {
//how do I set this field so that no matter what, I don't get an exeption?
long someLong = Long.parseLong(input.getSomeLong());
// how do I prevent the NumberFormatException when Long.parseLong() is called in this situation?
SomeInnerClass innerClass = SomeInnerClass(Long.parseLong(input.getSomeLong()));
// how do I prevent the NumberFormatException when Long.parseLong() is called in this situation?
innerClass.doStuff(Long.parseLong(input.getSomeLong()));
// ...
}
}
测试Class:
public class TestSomeClass {
@Tested private SomeClass classBeingTested;
@Injectable SomeInnerClass innerClass;
@Injectable InputClass input; // doesn't matter if I use @mocked here
@Test
public void shouldTestSomething() {
new NonStrictExpectations() {{
// some expectations with innerClass...
}};
classBeingTested.myMethod(input); // at this point I would get an error from Long.parsLong()
// ...
}
}
我希望能够忽略我正在测试的方法中某些代码部分的错误。在这种特定情况下,我根本不关心 Long.parseLong() 错误。我目前没有尝试测试它,但它妨碍了真正的测试。你如何创建一个假对象来代替引起问题的对象,以便在我测试该方法的其他部分时可以忽略它?
请使用下面的代码并检查它是否有帮助。您需要将 mockito 的依赖项添加到您的 project.One 重要的想法确保您在测试 class.
中使用 @RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.class)
public class TestSomeClass {
@Tested private SomeClass classBeingTested;
@Injectable SomeInnerClass innerClass;
@Mock
InputClass input; // doesn't matter if I use @mocked here
@Test
public void shouldTestSomething() {
new NonStrictExpectations() {{
// some expectations with innerClass...
}};
Mockito.when(input).getSomeLong().thenReturn("11").thenReturn("11");//This will return 11 in place of input.getSomeLong()for 2 times
classBeingTested.myMethod(input); // In this case yoou will not get an error from Long.parsLong()
// ...
}
}
============================================= ==========
使用 JMockit :
import org.junit.Test;
import mockit.Injectable;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.Tested;
public class SomeClassTest {
@Tested
private SomeClass classBeingTested;
@Injectable
SomeInnerClass innerClass;
@Mocked
InputClass input;
@Test
public void shouldTestSomething() {
new NonStrictExpectations() {
{
input.getSomeLong();
returns("11");
}
};
classBeingTested.myMethod(input);
// ...
}
}
我无法控制我想测试的方法中的变量发生了什么。在此示例中,被测试方法的输入是模拟对象或可注入对象,但如果您尝试对其执行任何操作,则会出现 Null* 异常。
Class及正在测试的方法:
public class SomeClass {
public SomeClass() {}
public void myMethod(InputClass input) {
//how do I set this field so that no matter what, I don't get an exeption?
long someLong = Long.parseLong(input.getSomeLong());
// how do I prevent the NumberFormatException when Long.parseLong() is called in this situation?
SomeInnerClass innerClass = SomeInnerClass(Long.parseLong(input.getSomeLong()));
// how do I prevent the NumberFormatException when Long.parseLong() is called in this situation?
innerClass.doStuff(Long.parseLong(input.getSomeLong()));
// ...
}
}
测试Class:
public class TestSomeClass {
@Tested private SomeClass classBeingTested;
@Injectable SomeInnerClass innerClass;
@Injectable InputClass input; // doesn't matter if I use @mocked here
@Test
public void shouldTestSomething() {
new NonStrictExpectations() {{
// some expectations with innerClass...
}};
classBeingTested.myMethod(input); // at this point I would get an error from Long.parsLong()
// ...
}
}
我希望能够忽略我正在测试的方法中某些代码部分的错误。在这种特定情况下,我根本不关心 Long.parseLong() 错误。我目前没有尝试测试它,但它妨碍了真正的测试。你如何创建一个假对象来代替引起问题的对象,以便在我测试该方法的其他部分时可以忽略它?
请使用下面的代码并检查它是否有帮助。您需要将 mockito 的依赖项添加到您的 project.One 重要的想法确保您在测试 class.
中使用@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.class)
public class TestSomeClass {
@Tested private SomeClass classBeingTested;
@Injectable SomeInnerClass innerClass;
@Mock
InputClass input; // doesn't matter if I use @mocked here
@Test
public void shouldTestSomething() {
new NonStrictExpectations() {{
// some expectations with innerClass...
}};
Mockito.when(input).getSomeLong().thenReturn("11").thenReturn("11");//This will return 11 in place of input.getSomeLong()for 2 times
classBeingTested.myMethod(input); // In this case yoou will not get an error from Long.parsLong()
// ...
}
}
============================================= ==========
使用 JMockit :
import org.junit.Test;
import mockit.Injectable;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.Tested;
public class SomeClassTest {
@Tested
private SomeClass classBeingTested;
@Injectable
SomeInnerClass innerClass;
@Mocked
InputClass input;
@Test
public void shouldTestSomething() {
new NonStrictExpectations() {
{
input.getSomeLong();
returns("11");
}
};
classBeingTested.myMethod(input);
// ...
}
}