繁体   English   中英

Mockito test case for class which calls another class in its constructor, in which 3 varibles are assigned inside them for legacy applicaiton in Java

[英]Mockito test case for class which calls another class in its constructor, in which 3 varibles are assigned inside them for legacy applicaiton in Java

我在下面有一个 class ,它在其构造函数中调用另一个 class ,其中分配了 3 个变量,当我尝试模拟它时,我得到

    java.lang.RuntimeException: Invoking the beforeTestMethod method on PowerMock test listener org.powermock.api.extension.listener.AnnotationEnabler@416dd4d8 failed.
    Caused by: org.mockito.exceptions.base.MockitoException: 
    Cannot instantiate @InjectMocks field named 'guidelinesProcessor' of type 'class com.psp.pca.businesslogic.GuidelinesProcessor'.
    You haven't provided the instance at field declaration so I tried to construct the instance.
    However the constructor or the initialization block threw an exception : null
Caused by: java.lang.NullPointerException
    at com.psp.pca.containers.GuidelineResultPageBean.<init>(GuidelineResultPageBean.java:8)
    at com.psp.pca.businesslogic.GuidelinesProcessor.<init>(GuidelinesProcessor.java:4)

指南Processor.java

public class GuidelinesProcessor extends LogicProcessor {

private GuidelineResultPageBean pb;

public GuidelinesProcessor() {
pb = new GuidelineResultPageBean();
} 
public void process(Context ctx, Locale loc, PropertyBundle rb) throws Exception {   
.....
}
}

GuidelineResultPageBean.java

public class GuidelineResultPageBean extends PageBean {
    private PackageDetails measurements;
    private Recommendation recom;
    private String guideline1 = null;
    private String guideline2 = null;
    private String guideline3 = null;

    public GuidelineResultPageBean() {      
        guideline1 = ResourceLoader.getProperties().getProperty(Constants.GUIDELINE1_STR);      
        guideline2 = ResourceLoader.getProperties().getProperty(Constants.GUIDELINE2_STR);
        guideline3 = ResourceLoader.getProperties().getProperty(Constants.GUIDELINE3_STR);
    }
}

常量.java

public class Constants {
public static final String GUIDELINE1_STR = "guideline.1";
    public static final String GUIDELINE2_STR = "guideline.2";
    public static final String GUIDELINE3_STR = "guideline.3";
}

在哪里

guideline.1 = "P1"
guideline.2 = "P2"
guideline.3 = "P3"

下面是我的测试 class:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ResourceLoader.class, Context.class, Logger.class })
public class GuidelinesProcessorTest {

PropertyBundle mockrb;

@Mock
Context ctx;    
@Mock
GuidelineResultPageBean pgbean;    

@InjectMocks
GuidelinesProcessor guidelinesProcessor;  


@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(ResourceLoader.class);  
mockrb = new PropertyBundle();
mockrb.setProperty(Constants.KEY_LWH_UOFM_ENGLISH_CA, "in");
mockrb.setProperty(Constants.KEY_WT_UOFM_ENGLISH, "lbs");
mockrb.setProperty(Constants.KEY_LWH_UOFM_ENGLISH, "inches");       
when("guideline.1").thenReturn("P1");
when("guideline.2").thenReturn("P2");
when("guideline.3").thenReturn("P3");
}

@Test
public void testProcess() throws Exception {
loc = new Locale("en", "US");

GuidelinesProcessor guidelinesProcessorSpy = Mockito.spy(new GuidelinesProcessor());
Mockito.doReturn(pgbean).when(guidelinesProcessorSpy);

guidelinesProcessor.process(pgf, loc, mockrb);
}
}

基本上它无法调用

PGAResourceLoader.getProperties().getProperty(Constants.GUIDELINE1_STR)

如果有人能用正确的方法帮助我 mocking 它会很有帮助。

您可以执行以下操作

@Mock
private Context ctx;   
 
@Mock
private GuidelineResultPageBean pgbean;    

private GuidelinesProcessor guidelinesProcessor;  


@Before
public void setUp() {
    guidelinesProcessor = PowerMockito.mock(GuidelinesProcessor.class, CALL_REAL_METHODS);
    Whitebox.setInternalState(guidelinesProcessor, "fieldName", pgbean);
    Whitebox.setInternalState(guidelinesProcessor, "fieldNameContext", ctx);
    // continue
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM