简体   繁体   中英

AssertionError when I try to write a unit test

I'm creating the processor to collect data and provide them in list. But when I'm trying to create test for my method i'm catching assertionerror. What am I doing wrong?

My Class:

@AllArgsConstructor
public class ZteProcessor implements OurProcessor {

    private final static String VENDOR = "ZTE";

    private String jsonString;
    private Map<String, String> metricsGU;
    private Map<String, String> metricsUMTS;
    private Map<String, String> metricsLTE;

    @Override
    public List<TimingAdvance> getTA() throws ParseException, NotFoundPatternOrMetricsException {
        TimeAdvanceDataStore data = new TimeAdvanceDataStore();
        AllDataFromJSONFile fromJSONFile = ProcessorUtil.getAllData(jsonString);
        if (jsonString.contains("String")) {
            return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsGU, 2);
        } else if (jsonString.contains("String-2")) {
            return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsUMTS, 3);
        } else if (jsonString.contains("String3")) {
            return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsLTE, 4);
        } else {
            throw new NotFoundPatternOrMetricsException();
        }
    }
}

My Test:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ProcessorUtil.class})
public class ZteProcessorTest {

    @Mock
    private AllDataFromJSONFile fromJSONFile;
    @Mock
    private TimeAdvanceDataStore data;

    private OurProcessor processor;

    private TimingAdvance timingAdvance = new TimingAdvance();

    private Map<String, String> metricsGU = new HashMap<>();
    private Map<String, String> metricsUMTS = new HashMap<>();
    private Map<String, String> metricsLTE = new HashMap<>();

    @Test
    public void getTATest() throws Exception {
        String jsonString = " { String : value}";
        processor = new ZteProcessor(jsonString, metricsGU, metricsUMTS, metricsLTE);
        List<TimingAdvance> list = new ArrayList<>();
        list.add(timingAdvance);
        PowerMockito.mockStatic(ProcessorUtil.class);

        when(ProcessorUtil.getAllData(jsonString)).thenReturn(fromJSONFile);
        when(data.allDataToTimingAdvance(jsonString, fromJSONFile, metricsGU, 2)).thenReturn(list);
        assertEquals(list, processor.getTA());
    }
}

Stacktrace:

java.lang.AssertionError: 
Expected :[TimingAdvance{filial='null', vendor='null', cellName='null', periodDate=null, taMetrics=null}]
Actual   :[]
<Click to see difference>


    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)

My ZteProcessor using static method getAllData(jsonString) of class ProcessorUtill . And for it I use powermock.

The problem stems from the fact that you are setting your expectations on a TimeAdvanceDataStore data mock, but you are creating a new instance of TimeAdvanceDataStore in your method under test.

Since you are already using PowerMockito, you can tap into new object creation like

PowerMockito.whenNew(TimeAdvanceDataStore.class)
            .withAnyArguments().thenReturn(data);

On top of that, think how many ZTEProcessors and TimeAdvanceDataStores you have in your app. Do you always want a new instance of TimeAdvanceDataStore in each call to getTA?

If not, just pass TimeAdvanceDataStore in a constructor.

If yes, common approaches when PowerMockito is not at your disposal are:

  • passing a factory of TimeAdvanceDataStore to ZTEProcessor constructor
  • passing TimeAdvanceDataStore to getTA method
  • extracting a method constructing TimeAdvanceDataStore and overriding it in test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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