[英]EasyMock and parameterized tests (JUnit parameterized)
我想在參數化測試類中的類上使用@Mock。 但是由於某些原因,mockClassB為NULL。 我的代碼類似於
@RunWith(Parameterized.class)
public class ClassATest extends EasyMockSupport {
private String uniqueIdentifier;
private String value;
@Mock
private ClassB mockClassB;
public ClassATest(String uniqueIdentified, String value) {
this.uniqueIdentifier = uniqueIdentified;
this.value = value;
}
...
@Parameterized.Parameters(name = "{index}: id = {0}; value = {1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"1", "val1"},
{"2", "val2"}});
}
@Test
public void testMethod() {
...
expect(mockClassB.someMethod(uniqueIdentifier)).andReturn(value); // mockClassB is NULL
replayAll();
....
}
}
是否可以在參數化類中創建模擬對象?
嘗試添加setUp方法:
@Before
public void setUp() {
initMocks(this);
}
正確的代碼將在下面顯示
@RunWith(Parameterized.class)
public class ClassATest extends EasyMockSupport {
private String uniqueIdentifier;
private String value;
private ClassB mockClassB;
public ClassATest(String uniqueIdentified, String value) {
this.uniqueIdentifier = uniqueIdentified;
this.value = value;
}
...
@Parameterized.Parameters(name = "{index}: id = {0}; value = {1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"1", "val1"},
{"2", "val2"}});
}
@Before
public void setup() {
...
mockClassB = mock(ClassB.class);
...
}
@Test
public void testMethod() {
...
expect(mockClassB.someMethod(uniqueIdentifier)).andReturn(value); // mockClassB will be mocked :)
replayAll();
....
}
}
EasyMock需要規則或運行器來實例化帶注釋的模擬。 由於您已經在使用跑步者,因此唯一的選擇就是規則。 以下將起作用。
@RunWith(Parameterized.class)
public class ClassATest extends EasyMockSupport {
@Rule
public EasyMockRule rule = new EasyMockRule(this);
private String uniqueIdentifier;
private String value;
@Mock
private ClassB mockClassB;
public ClassATest(String uniqueIdentified, String value) {
this.uniqueIdentifier = uniqueIdentified;
this.value = value;
}
@Parameterized.Parameters(name = "{index}: id = {0}; value = {1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"1", "val1"},
{"2", "val2"}});
}
@Test
public void testMethod() {
expect(mockClassB.someMethod(uniqueIdentifier)).andReturn(value); // mockClassB is NULL
replayAll();
}
}
另一種方法是直接調用EasyMockSupport
上可用的injectMocks
。
@RunWith(Parameterized.class)
public class ClassATest extends EasyMockSupport {
private String uniqueIdentifier;
private String value;
@Mock
private ClassB mockClassB;
public ClassATest(String uniqueIdentified, String value) {
this.uniqueIdentifier = uniqueIdentified;
this.value = value;
}
@Before
public void before() {
injectMocks(this);
}
@Parameterized.Parameters(name = "{index}: id = {0}; value = {1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"1", "val1"},
{"2", "val2"}});
}
@Test
public void testMethod() {
expect(mockClassB.someMethod(uniqueIdentifier)).andReturn(value); // mockClassB is NULL
replayAll();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.