【问题标题】:Compare the equality or no-equality bewteen two getters with Mockito用 Mockito 比较两个 getter 之间的相等或不相等
【发布时间】: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 方法中没有返回语句。可以看看吗?
  • 我已经用整个功能编辑了帖子!这就是你想要的?
  • 是的,很好。你能分享一下你也遇到过的错误吗?

标签: spring mockito


【解决方案1】:

如果我不清楚您的问题,请在 cmets 中告诉我。

使用 Mockito 的目的是将您的特定单元与其他依赖项隔离开来。这就是为什么我认为你应该用@Mock 注释标记CompteManagerOuvrageManager

然后尝试使用这种方式来模拟方法调用。而不是:

Mockito.doReturn(ouvrage).when(ouvrageManager).get(id);
Mockito.doReturn(compte).when(compteManager).get(id);

这样使用:

Mockito.when(ouvrageManager.get(id)).thenReturn(ouvrage);
Mockito.when(compteManager.get(id)).thenReturn(compte); 

【讨论】:

  • 我已经对经理和两条线进行了@Mock 更改!但我仍然收到错误,如果你能看到,我已经用新错误的图像编辑了我的帖子。
  • 你需要设置idOuvrage,错误信息是这样的
  • 好的,我已经为 ExportShapeAction 添加了代码并执行了 exportShapeManager.setIdOuvrage(id);并编辑了我的代码,试图为 ouvrage.getSociete() 和 compte.getSociete() 赋予价值,但现在我有一个 nullPointerException ...
  • @AloïsCoussout 如果没有最新的代码库,很难找到错误
  • 我已经编辑了 classTest 的帖子,您缺少信息吗?
猜你喜欢
  • 2010-09-24
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多