简体   繁体   中英

junit.framework.AssertionFailedError and no runnable methods because of whitespace

I am getting a junit.framework.AssertionFailedError when I do this:

assertEquals("123 \t July \t testuser \t USD \t one two \t pnl \t ACTUALS_STAGE \t product_line \t pnl", tabs );

The error:

expected:<123[   July    testuser    USD     one two     pnl     ACTUALS_STAGE   product_line    ]pnl>
but was:<123[   July    testuser    USD one two pnl ACTUALS_STAGE   product_line    ]pnl>

"tabs" is getting the tab-separated parameters from the function being tested. The assertion is failing because of whitespace issue. Whitespaces are getting appended between the values alongwith a tab.

Test code:

@Before
    public void setUp() throws Exception {
        List<String> list = Arrays.asList("one", "two");
        List<String> scenarioList = Arrays.asList("ACTUALS_STAGE");
        List<Map<String, List<String>>> filter = new ArrayList<>();
        Map<String, List<String>> filtersMap = new HashMap<>();
        List<String> filterList = Arrays.asList("A1PCON");
        filtersMap.put("product_line", filterList);
        filter.add(filtersMap);

        reportQueryParams = ReportQueryParams.builder()
                .id("123").currency("USD").companies(list).cognosDatasetType("pnl").scenarios(scenarioList)
                .filters(filter).reportTemplate("pnl")
                .build();
    }

    @Test
    public void tabSerializerTest() {
        String remoteUser = "testuser";
        HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpServletRequest.getRemoteUser())
               .thenReturn(remoteUser);
        MetricsSerializer metricsSerializer = new MetricsSerializer();
        String tabs = metricsSerializer.serializeMetrics(reportQueryParams, httpServletRequest);
        assertEquals("123 \t July \t testuser \t USD \t one two \t pnl \t ACTUALS_STAGE \t product_line \t pnl", tabs );
    }

For example:

abc \\t pqr : this will display the first value, add whitespace, add tab, add whitespace and then display second value.

Expected: abc\\tpqr

If I do "abc\\tpqr", I get the following error:

No runnable method found.

I have annotated it with @Test and it works if I do "abc \\t pqr"

I also tried doing "abc" + "\\t" "pqr" but it gives the same runnable not found (initialization error).

How can I deal with this to fix the assertion error and the runnable error? I just want a tabspace between the values.

Do you really need to check the tab and white-spaces? If not then pass your string result to a new method that will fix the spacing issue. Or you can simply return an array of values, ignoring the white-spaces, like below.

public static String[] getValues(String input){
  //tokenize 
  String[] values = input.split("\\s+");  
  return values;    

}

You can then use the Assertion to compare arrays of expected and actual.

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