So I have 2 pieces of Java code. One is a simple little piece of source code and the other is a JUnit test class that I'm trying to mess around with. However, whenever I want to debug the code to make sure that this simple code is appearing correctly, I get a NullPointerException at the java.util.ArrayList.forEach(ArrayList.java:1507) error.
Here's my source code:
public class sourceCode {
public int mid(int x, int y, int z) {
int m = z;
if (y < z) {
if (x < y) {
m = y;
}
else if (x < z) {
m = x;
}
}
else {
if (x > y) {
m = y;
}
else if (x > z) {
m = x;
}
}
return m;
}
}
Here is my JUnit Test Case code:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.jupiter.api.Test;
public class sourceCodeTest {
public sourceCode sourceCodeVar;
//Test Fixture
@Before
public void setUpBeforeClass() throws Exception {
sourceCodeVar = new sourceCode();
}
@Test
void test() {
//Test Oracle
int oracle = 2;
//Test Case
int middleTest1 = sourceCodeVar.mid(1, 2, 3);
assertEquals(oracle, middleTest1);
}
}
Findings
They are both different files under the same directory, they were created in Eclipse in a project as well. I saw in another slightly related thread that it may be a bug related to Eclipse however upon testing it on VS Code, the same error appears which would indicate that it's an error on my part.
I guess the reason why I'm so confused is that the code seems to be so simple that I can't pinpoint where I messed up.
Aucun commentaire:
Enregistrer un commentaire