Skip to main content
edited body
Source Link
dustytrash
  • 2.4k
  • 8
  • 17

Since you mentioned unit tests, here are 2 unit tests that proves your code does not work (Based on name, age, email, phone header):

@Test public void testFindIndicesThrowsException() { ClassName x = new ClassName(); try { int[] result = x.findIndices("Name", "age", "FieldThatDoesNotExist", "Phone"); fail("expected exception was not thrown!"); } catch (IllegalArgumentException exception) { assertEquals("One or more of input arguments could not be found in the headers.", exception.getMessage()); } } @Test public void testFindIndices() { ClassName x = new ClassName(); int[] result = x.findIndices("Email", "Phone", "Name", "age"); assertEquals(4, result.length); assertEquals(02, result[0]); assertEquals(13, result[1]); assertEquals(20, result[2]); assertEquals(31, result[3]); } 

Explanation: You are setting a boolean in a loop. This means it could be set to false on one iteration, then set to true on the next.

Instead, you can set the index to the one found, and if it's not found, throw an error:

int k = 0; for (String s : input) { indices[k] = -1; for (int i = 0; i < this.headers.length; i++) { if (s.equalsIgnoreCase(this.headers[i])) { indices[k] = i; } } // If we never found the item if (indices[k] == -1) { throw new IllegalArgumentException( "One or more of input arguments could not be found in the headers."); } k++; } 

Since you mentioned unit tests, here are 2 unit tests that proves your code does not work (Based on name, age, email, phone header):

@Test public void testFindIndicesThrowsException() { ClassName x = new ClassName(); try { int[] result = x.findIndices("Name", "age", "FieldThatDoesNotExist", "Phone"); fail("expected exception was not thrown!"); } catch (IllegalArgumentException exception) { assertEquals("One or more of input arguments could not be found in the headers.", exception.getMessage()); } } @Test public void testFindIndices() { ClassName x = new ClassName(); int[] result = x.findIndices("Email", "Phone", "Name", "age"); assertEquals(4, result.length); assertEquals(0, result[0]); assertEquals(1, result[1]); assertEquals(2, result[2]); assertEquals(3, result[3]); } 

Explanation: You are setting a boolean in a loop. This means it could be set to false on one iteration, then set to true on the next.

Since you mentioned unit tests, here are 2 unit tests that proves your code does not work (Based on name, age, email, phone header):

@Test public void testFindIndicesThrowsException() { ClassName x = new ClassName(); try { int[] result = x.findIndices("Name", "age", "FieldThatDoesNotExist", "Phone"); fail("expected exception was not thrown!"); } catch (IllegalArgumentException exception) { assertEquals("One or more of input arguments could not be found in the headers.", exception.getMessage()); } } @Test public void testFindIndices() { ClassName x = new ClassName(); int[] result = x.findIndices("Email", "Phone", "Name", "age"); assertEquals(4, result.length); assertEquals(2, result[0]); assertEquals(3, result[1]); assertEquals(0, result[2]); assertEquals(1, result[3]); } 

Explanation: You are setting a boolean in a loop. This means it could be set to false on one iteration, then set to true on the next.

Instead, you can set the index to the one found, and if it's not found, throw an error:

int k = 0; for (String s : input) { indices[k] = -1; for (int i = 0; i < this.headers.length; i++) { if (s.equalsIgnoreCase(this.headers[i])) { indices[k] = i; } } // If we never found the item if (indices[k] == -1) { throw new IllegalArgumentException( "One or more of input arguments could not be found in the headers."); } k++; } 
added another example test
Source Link
dustytrash
  • 2.4k
  • 8
  • 17

Since you mentioned unit tests, here's an examplehere are 2 unit testtests that proves your code does not work (Based on name, age, email, phone header):

@Test public void testFindIndicesThrowsException() { ClassName x = new ClassName(); try { int[] result = x.findIndices("Name", "age", "FieldThatDoesNotExist", "Phone"); fail("expected exception was not thrown!"); } catch (IllegalArgumentException exception) { assertEquals("One or more of input arguments could not be found in the headers.", exception.getMessage()); } } @Test public void testFindIndices() { ClassName x = new ClassName();   int[] result = x.findIndices("Email", "Phone", "Name", "age"); assertEquals(4, result.length); assertEquals(0, result[0]); assertEquals(1, result[1]); assertEquals(2, result[2]); assertEquals(3, result[3]); } 

Explanation: You are setting a boolean in a loop. This means it could be set to false on one iteration, then set to true on the next.

Since you are throwing an exception anyway, you could throw it right away, or set your boolean & break out of the loop.

Since you mentioned unit tests, here's an example unit test that proves your code does not work:

@Test public void testFindIndicesThrowsException() { ClassName x = new ClassName(); try { int[] result = x.findIndices("Name", "age", "FieldThatDoesNotExist", "Phone"); fail("expected exception was not thrown!"); } catch (IllegalArgumentException exception) { assertEquals("One or more of input arguments could not be found in the headers.", exception.getMessage()); } } 

Explanation: You are setting a boolean in a loop. This means it could be set to false on one iteration, then set to true on the next.

Since you are throwing an exception anyway, you could throw it right away, or set your boolean & break out of the loop.

Since you mentioned unit tests, here are 2 unit tests that proves your code does not work (Based on name, age, email, phone header):

@Test public void testFindIndicesThrowsException() { ClassName x = new ClassName(); try { int[] result = x.findIndices("Name", "age", "FieldThatDoesNotExist", "Phone"); fail("expected exception was not thrown!"); } catch (IllegalArgumentException exception) { assertEquals("One or more of input arguments could not be found in the headers.", exception.getMessage()); } } @Test public void testFindIndices() { ClassName x = new ClassName();   int[] result = x.findIndices("Email", "Phone", "Name", "age"); assertEquals(4, result.length); assertEquals(0, result[0]); assertEquals(1, result[1]); assertEquals(2, result[2]); assertEquals(3, result[3]); } 

Explanation: You are setting a boolean in a loop. This means it could be set to false on one iteration, then set to true on the next.

Source Link
dustytrash
  • 2.4k
  • 8
  • 17

Since you mentioned unit tests, here's an example unit test that proves your code does not work:

@Test public void testFindIndicesThrowsException() { ClassName x = new ClassName(); try { int[] result = x.findIndices("Name", "age", "FieldThatDoesNotExist", "Phone"); fail("expected exception was not thrown!"); } catch (IllegalArgumentException exception) { assertEquals("One or more of input arguments could not be found in the headers.", exception.getMessage()); } } 

Explanation: You are setting a boolean in a loop. This means it could be set to false on one iteration, then set to true on the next.

Since you are throwing an exception anyway, you could throw it right away, or set your boolean & break out of the loop.