java - How can I create a JUnit test to test CompareTo? -
in code have 2 static methods. 1 using compare contents of 2 arrays of int's, , 1 comparing contents of 2 arrays of strings (in same order). how can structure junit test 1 of these methods? i'm thinking use edit** created test works not accurate.
//edited test, passes isn't correct. public class arraycomparertests { @test public void testintarray() { // arraycomparer arraycomparer = new arraycomparer(); int[] list1 = {2,2,3}; int[] list2 = {1}; assertequals(false, arraycomparer.compareintarrays(list1, list2)); } } public class arraycomparer { public static boolean compareintarrays(int[] list1, int[] list2) { // checks same array reference if (list1 == list2) { return true; } // checks null arrays if (list1 == null || list2 == null) { return false; } // arrays should of equal length if (list2.length != list1.length) { return false; } // compare array values (int = 0; < list1.length; i++) { (i = 0; < list2.length; i++) { if (list1[i] != list2[i]) return false; } } return true; } public static boolean comparestringarrays(string[] list3, string[] list4) { // checks same array reference if (list3 == list4) { return true; } // checks null arrays if (list3 == null || list4 == null) { return false; } // arrays should of equal length if (list4.length != list3.length) { return false; } // compare array values (int = 0; < list3.length; i++) { (i = 0; < list4.length; i++) { if (list3[i] != list4[i]) return false; } } return true; } }
you can use assert.assertarrayequals.
@test public void testintarray() { int[] list1 = {2,2,3}; int[] list2 = {1}; // assertion fail assert.assertarrayequals(list2, list1); // assertion pass int list3 = {2,2,3}; assert.assertarrayequals(list1, list3); }
Comments
Post a Comment