【发布时间】:2021-06-18 09:31:02
【问题描述】:
大家好,我刚开始在一个项目中使用 Mockito 进行模拟,但我显然什么都不懂……我只想检查是否有两个 getter(ouvrage.getSociete() 和 compte.getSociete()) else if() 等于或不等于看后处理。
但是在我的测试课中,我试图给它们赋予相同的价值,但仅仅赋予它们价值已经存在一些问题。所以我认为比较也不会那么容易......
我要检查的 ExportShapeAction:
@Namespace("/front")
@ParentPackage("gu-default")
@InterceptorRefs({
@InterceptorRef("guStack")
})
@Controller
@Scope("prototype")
public class ExportShapeAction extends FrontAction {
private static final long serialVersionUID = -3228513581235888117L;
@Autowired
private JetonAccesService jetonAccesService;
@Autowired
private OuvrageManager ouvrageManager;
@Autowired
private OuvrageSasManager ouvrageSasManager;
private Long idOuvrage;
@org.apache.struts2.convention.annotation.Action(value = "exportShape", results = {
@Result(name = Action.SUCCESS, params = { "actionName", "mesouvrages", "namespace", "/front" }, type = "redirectAction"),
@Result(name = Action.ERROR, location = "/WEB-INF/jsp/front/mesouvrages/mesouvrages.jsp"),
})
public String demandeExportShape() {
Compte compte = getCompteCourant();
Ouvrage ouvrage = ouvrageManager.get(idOuvrage);
if (ouvrage == null) {
this.addActionError("Export Shape depuis la publication impossible : l'ouvrage n'est pas encore publié");
return Action.ERROR;
} else if (!ouvrage.getSociete().equals(compte.getSociete())) {
this.addActionError("Vous n'avez pas les droits requis sur l'ouvrage");
return Action.ERROR;
}
try {
jetonAccesService.demandeExportZoneOuvrage(compte.getCompteId(), idOuvrage, false, false);
this.addActionMessage("Votre demande d’export est enregistrée. Vous recevrez un courriel de confirmation avec le lien de téléchargement lorsque celle-ci sera finalisée.");
} catch (FonctionnelleException e) {
// specification du message
if ("Doublon".equalsIgnoreCase(e.getTitre())) {
List<String> messages = new ArrayList<>();
messages.add("Une demande d’export est déjà en cours de traitement.");
e.setMessages(messages);
}
this.addFonctionnelleExceptionToActionErrors(e);
return Action.ERROR;
}
return Action.SUCCESS;
}
我用 mockito 创建的 ExportShapeActionTest 类
@ExtendWith(MockitoExtension.class)
@ContextConfiguration(classes = ExportShapeActionTest.class)
public class ExportShapeActionTest {
@InjectMocks
private ExportShapeAction exportShapeManager;
@Spy
private CompteManager compteManager;
@Spy
private SocieteManager societeManager;
@Spy
private OuvrageManager ouvrageManager;
public void testDemandeExportShapeParamOkOuvrageOkCompteOk() {
Societe societe1 = new Societe();
Societe societe2 = new Societe();
Long id = new Long(4);
exportShapeManager.setIdOuvrage(id);
Ouvrage ouvrage = Mockito.mock(Ouvrage.class);
Compte compte = Mockito.mock(Compte.class);
Mockito.when(ouvrage.getSociete()).thenReturn(societe1);
Mockito.when(compte.getSociete()).thenReturn(societe2);
// Act
String result = exportShapeManager.demandeExportShape();
// Assert
Assertions.assertEquals("error", result);
【问题讨论】:
-
demandeExportShape方法中没有返回语句。可以看看吗? -
我已经用整个功能编辑了帖子!这就是你想要的?
-
是的,很好。你能分享一下你也遇到过的错误吗?