有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Mockito类的测试用例,该类在其构造函数中调用另一个类,其中为Java中的遗留应用程序分配了3个变量

下面有一个类,它在构造函数中调用另一个类,其中分配了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)

指南处理器。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 {   
.....
}
}

指南ResultPageBean。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"

下面是我的测试课:

@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)

如果有人能用正确的方式来嘲笑我的话,那将是非常有帮助的


共 (1) 个答案

  1. # 1 楼答案

    你可以做以下事情

    @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
    }