There is a method to which you need to write a unit-test, which will take into account the four possible values ​​of newByte (-1, 0, 1, and any other number).

Since the string for the compiler is not covered by dough

if ((newByte == 0) || (newByte == 1) || (newByte == -1)) 

The method itself:

 public static byte primConvertToByte(final byte changeByte) { // byte values [-128, 127] byte newByte = changeByte; // error values: -1, 0, 1 because of involution changeInt.Solve for adding TWO. if ((newByte == 0) || (newByte == 1) || (newByte == -1)) { newByte += (byte) PrimitiveConvensionRunner.TWO; } newByte *= PrimitiveConvensionRunner.TWO; return newByte; } 

Here is an example of my test that needs to be correctly corrected for these values ​​(I only have one).

 @Test public void testPrimConvertToByte() { final byte startValue = 1; final byte expectedByteValueFirst = 6; new PrimitiveConvensionRunner(); Assert.assertEquals("Test for Byte", expectedByteValueFirst, PrimitiveConvensionRunner.primConvertToByte(startValue)); } 
  • one
    and what prevents you from trying to write three more asserts for the remaining three values? - Nofate
  • @Nofate With this method, for the compiler, the line of code (newByte == 0) || (newByte == 1) || (newByte == -1) remains uncovered (2 of 3 branches are missed) - GitHors

1 answer 1

For each case - a separate test. There are five equivalence groups in this method: less than -1, -1, 0, 1, more than 1. For each there should be a separate test in the overall test set.

The option is simple, but violates DRY:

 public void testPrimConvertToByte(byte startValue, byte expected, string message) { new PrimitiveConvensionRunner(); Assert.assertEquals(message, expected, PrimitiveConvensionRunner.primConvertToByte(startValue)); } @Test public void testPrimConvertToByteNegative() { testPrimConvertToByte(1, 6, "should return 6 for argument equal to 1"); } @Test public void testPrimConvertToByteMinusOne() { testPrimConvertToByte(-1, 100500, "should return 100500 for argument equal to -1"); } @Test public void testPrimConvertToByteOne() { testPrimConvertToByte(42, 1337, "should return leet value for argument more than 1"); } ... 

The variant is more difficult, but laconic:

Write data-driven tests.