0

My test case looks like

public class CheckSortedList {

List<String> expectedList;
ModelController modelController;
IDataReader dataReader;
List<String> tempBookNames = new LinkedList<>();

public CheckSortedList() {
    expectedList = new LinkedList<>();
    try {
        dataReader = new RawDataReader();
        modelController = new ModelController(dataReader);

    } catch (Exception e) {
        fail(e.getMessage());
    }
}

@Before
public void init() {
        // sortierte Liste aus Excel
        expectedList.add("Das große GU-Kochbuch Kochen für Kinder");
        expectedList.add("Das Perfekte Dinner. Die besten Rezepte");
        expectedList.add("Das Piratenkochbuch. Ein Spezialitätenkochbuch mit den 150 leckersten Rezepten ");
        expectedList.add("Genial italienisch");
        expectedList.add("Ich helf dir kochen. Das erfolgreiche Universalkochbuch mit großem Backteil");
        expectedList.add("O'Reillys Kochbuch für Geeks");
        expectedList.add("Schlank im Schlaf ");
        expectedList.add("Schuhbecks Kochschule. Kochen lernen mit Alfons Schuhbeck ");
        modelController.getSortedBooks().forEach(book -> tempBookNames.add(book.getTitle()));
}

@Test
public void test() {

    tempBookNames.forEach(s -> {
        System.out.println(s);
    });
    assertEquals(expectedList, tempBookNames);  

}

}

logically every thing should work fine since the result book list is the same as expected but i have noticed that the Assert.assertEqual(Object[] array) is deprecated. My test case fails and i can not figure out why

the output is `java.lang.AssertionError: expected:<[Das große GU-Kochbuch Kochen für Kinder, Das Perfekte Dinner. Die besten Rezepte, Das Piratenkochbuch. Ein Spezialitätenkochbuch mit den 150 leckersten Rezepten , Genial italienisch, Ich helf dir kochen. Das erfolgreiche Universalkochbuch mit großem Backteil, O'Reillys Kochbuch für Geeks, Schlank im Schlaf , Schuhbecks Kochschule. Kochen lernen mit Alfons Schuhbeck ]> but was:<[Das Perfekte Dinner. Die besten Rezepte, Das Piratenkochbuch. Ein Spezialitätenkochbuch mit den 150 leckersten Rezepten , Das große GU-Kochbuch Kochen für Kinder, Genial italienisch, Ich helf dir kochen. Das erfolgreiche Universalkochbuch mit großem Backteil, O'Reillys Kochbuch für Geeks, Schlank im Schlaf , Schuhbecks Kochschule. Kochen lernen mit Alfons Schuhbeck ]> 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) at de.optivo.buecherverwaltung.test.CheckSortedList.test(CheckSortedList.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

the output of sysout of the retrieved listDas Perfekte Dinner. Die besten Rezepte Das Piratenkochbuch. Ein Spezialitätenkochbuch mit den 150 leckersten Rezepten Das große GU-Kochbuch Kochen für Kinder Genial italienisch Ich helf dir kochen. Das erfolgreiche Universalkochbuch mit großem Backteil O'Reillys Kochbuch für Geeks Schlank im Schlaf Schuhbecks Kochschule. Kochen lernen mit Alfons Schuhbeck ` the lists are identical !

FarFarAway
  • 1,017
  • 3
  • 14
  • 35
  • What is the error you get? It should tell you how the two lists differ. And even if it doesn't, you could just use your debugger, or even simple System.out.println(), to know what the two lists contain. – JB Nizet Oct 29 '16 at 07:35
  • I have use sysout to check both lists. they are identical . the test case fails and the error is AssertionError – FarFarAway Oct 29 '16 at 07:41
  • As I said, you should have a more precise output showing what the error is, how the lists differ. Add System.out.println to the code, and post the entire output of these statements, and the complete error message. We have no output, and have no idea of what your code does. – JB Nizet Oct 29 '16 at 07:43
  • 1
    Now read it. You expect the list to contain "Das große GU-Kochbuch Kochen für Kinder" as its first element, but it actually contains "Das Perfekte Dinner. Die besten Rezepte" as its first element. There might be other differences but I didn't read further. – JB Nizet Oct 29 '16 at 07:55
  • but my code to sort the list is public List getSortedBooks() { return books.stream().sorted((book1, book2) -> book1.getTitle().compareTo(book2.getTitle())) .collect(Collectors.toList()); } it should show the same output like excel right ? – FarFarAway Oct 29 '16 at 07:59
  • Maybe excel does a case insensitive sort and therefore sorts `Das grosse...` before `Das Perfekte...`, while java does a case sensitive sort, where `P` comes before `g` – Thomas Kläger Oct 29 '16 at 08:01
  • I have no idea what Excel uses to sort strings. I know what String.compareTo does, though, because it's explicitly described in the javadoc: it sorts by lexicographic order. P comes before g in the lexicographic order. So either you want lexicographic order, and your test is wrong, or you want another order, and your code is wrong. You probably want to use a Collator, or String.CASE_INSENSITIVE_ORDER. – JB Nizet Oct 29 '16 at 08:04

1 Answers1

0

the result after the contribution of Thomas Kläger and JB Nizet 4 is by replacing compareto in lamda expression with compareToIgnoreCase(). for furthre readings consider Sorting arraylist in alphabetical order (case insensitive) andCase sensitive/insensitive Sort in Excel (bug or a feature)

Community
  • 1
  • 1
FarFarAway
  • 1,017
  • 3
  • 14
  • 35
  • Probably nobody thinks your answer is worth a downvote, but in order to make a serious answer: dont just quote comments. It is fair to take that input and turn it into some real, complete code example. But just saying "as those other people say" doesnt result in a good answer. Then: mind your formatting and avoid typos (or omitted spaces) – GhostCat Oct 29 '16 at 13:55