我是JUnit的新手,正在尝试学习。我们如何为void方法编写JUnit。 我有一个无效的删除方法。我正在像下面那样编写测试,但无法正常工作。 请提出建议。
我必须为其编写测试用例的方法。
public class DoseService {
@Autowired
private DoseRepository doseRepo;
public void deleteDose(int id) {
doseRepo.deleteById(id);
}
}
我的测试
@Autowired
private DoseService doseService;
@MockBean
public DoseRepository doseRepository;
@Test
public void testDeleteDose() {
Dose dose = createDose();
ArgumentCaptor<Dose> arg = ArgumentCaptor.forClass(Dose.class);
doseService.deleteDose(dose.getDoseId());
verify(doseRepository).deleteById(arg.capture().getDoseId());
assertEquals("120", arg.getValue().getDoseValue());
}
private Dose createDose() {
Dose dose = new Dose();
dose.setDoseId(1);
dose.setDoseValue("120");
return dose;
}
预先感谢
已更新
java.lang.NullPointerException
at verify(doseRepository).deleteById(arg.capture().getDoseId());
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at
java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
答案 0 :(得分:1)
单元测试仅应测试为其编写的类。因此,无需注入真实对象,仅需模拟即可。另外,您不需要Dose对象,因为您唯一需要做的就是测试DoseService对象。
我将执行以下操作:
@ExtendWith(MockitoExtension.class)//for JUnit 5, but you can add the JUnit 4 annotation
public class DoseServiceTest {
@Mock
private DoseRepository doseRepository;
@InjectMocks
private DoseService sut;//System Under Test
@Test
public void testDeleteDose() {
int doseId=42;
// perform the call
sut.deleteDose(doseId);
// verify the mocks
verify(doseRepository, times(1)).deleteById(eq(doseId));
}
}
答案 1 :(得分:0)
这是因为您正在验证ASCII code 128 is character \200
ASCII code 129 is character \201
ASCII code 130 is character \202
ASCII code 131 is character \203
ASCII code 132 is character \204
ASCII code 133 is character \205
ASCII code 134 is character \206
ASCII code 135 is character \207
ASCII code 136 is character \210
ASCII code 137 is character \211
ASCII code 138 is character \212
ASCII code 139 is character \213
ASCII code 140 is character \214
ASCII code 141 is character \215
ASCII code 142 is character \216
ASCII code 143 is character \217
ASCII code 144 is character \220
ASCII code 145 is character \221
ASCII code 146 is character \222
ASCII code 147 is character \223
ASCII code 148 is character \224
ASCII code 149 is character \225
ASCII code 150 is character \226
ASCII code 151 is character \227
ASCII code 152 is character \230
ASCII code 153 is character \231
ASCII code 154 is character \232
ASCII code 155 is character \233
ASCII code 156 is character \234
ASCII code 157 is character \235
ASCII code 158 is character \236
ASCII code 159 is character \237
ASCII code 160 is character \240
ASCII code 161 is character \241
ASCII code 162 is character \242
ASCII code 163 is character \243
ASCII code 164 is character \244
ASCII code 165 is character \245
ASCII code 166 is character \246
ASCII code 167 is character \247
ASCII code 168 is character \250
ASCII code 169 is character \251
ASCII code 170 is character \252
ASCII code 171 is character \253
ASCII code 172 is character \254
ASCII code 173 is character \255
ASCII code 174 is character \256
ASCII code 175 is character \257
ASCII code 176 is character \260
ASCII code 177 is character \261
ASCII code 178 is character \262
ASCII code 179 is character \263
ASCII code 180 is character \264
ASCII code 181 is character \265
ASCII code 182 is character \266
ASCII code 183 is character \267
ASCII code 184 is character \270
ASCII code 185 is character \271
ASCII code 186 is character \272
ASCII code 187 is character \273
ASCII code 188 is character \274
ASCII code 189 is character \275
ASCII code 190 is character \276
ASCII code 191 is character \277
ASCII code 192 is character \300
ASCII code 193 is character \301
ASCII code 194 is character \302
ASCII code 195 is character \303
ASCII code 196 is character \304
ASCII code 197 is character \305
ASCII code 198 is character \306
ASCII code 199 is character \307
方法,但是在代码中,您正在使用delete
方法。
此代码应该有效:
deleteById
答案 2 :(得分:0)
在模拟操作中,您需要捕获参数。然后,您可以根据捕获的参数来声明所需的任何内容。
也许是这样吗?
verify(doseRepository).deleteById(arg.capture());
assertEquals(dose.getDoseId(), arg.getValue().getDoseId());