admin管理员组文章数量:1308163
I'm working on a class assignment to which we are using junit to test our code. I have three test failing and I'm not sure how to fix it. I am not sure if it is the test that I have coded wrong or my methods. my task creation, updated name, and updated description are the ones that are failing. but my unique id & deletions work. Below are my methods
public class TaskService {
//variables
public static String taskId;
public String taskName;
public String taskDescrip;
//create an array for task list
static ArrayList<Task> taskList = new ArrayList<Task>(0);
//creates unique IDs
public static String generateUniqueId() {
String uniqueId;
//pulls the latest Id value in array as long as its not empty
if (taskList.isEmpty()) {
taskId = "1000000000";
}
else {
int arraySize = taskList.size();
taskId = taskList.get(arraySize - 1).getId();
}
// this will convert the id string into a integer to increment (making new id) then change it back to a string
int tempInt = Integer.valueOf(taskId);
tempInt += 1;
uniqueId = Integer.toString(tempInt);
return uniqueId;
}
//creates task
public static void addTask(String name, String descrip) {
String Id = generateUniqueId();
Task Task1 = new Task(Id, name, descrip);
taskList.add(Task1);
}
//add task
public static void addTask(Task newTask) {
String tempId = newTask.getId();
for (int i = 0; i < taskList.size(); i++) {
if (tempId.equals(taskList.get(i).getId())) {
throw new IllegalArgumentException("Id must be unique");
}
}
taskList.add(newTask);
}
//updates name
public static void updateName(String uniqueId, String taskName) {
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
taskList.get(i).setName(taskName);
}
}
}
//updates description
public static void updateDescrip(String uniqueId, String taskDescrip) {
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
taskList.get(i).setName(taskDescrip);
}
}
}
//deletes task
public static void deleteTask(String uniqueId) {
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
int position = i;
taskList.remove(position);
}
}
}
//search function for testing
public static int searchTask(String uniqueId) {
int result = 0;
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
result = 1;
}
else {
result = 2;
}
}
return result;
}
}
and this is what I have written for the tests.
public class TaskServiceTest {
//Creation Test
@Test
@DisplayName("Task Creation")
void testTaskServiceClass() {
TaskService.addTask("Task Name", "Task Description");
assertTrue(TaskService.taskList.get(0).getId().equals("1000000001"));
assertTrue(TaskService.taskList.get(0).getName().equals("Task Name"));
assertTrue(TaskService.taskList.get(0).getDescrip().equals("Task Description"));
}
//Deletion Test
@Test
@DisplayName("Deletion Test")
void testTaskServiceDelete() {
TaskService.addTask("Task Service", "Testing Task Service");
int size = TaskService.taskList.size();
TaskService.deleteTask("1000000002");
assertTrue(TaskService.searchTask("1000000002") == 2);
}
//Test- update name
@Test
@DisplayName("Update name")
void testTaskServiceUpdateName() {
TaskService.addTask("Test 1", "Description");
int size = TaskService.taskList.size();
System.out.println(TaskService.taskList.get(size - 1).getId());
System.out.println(TaskService.taskList.get(size - 1).getName());
TaskService.updateName("1000000002", "Update Name");
System.out.println(TaskService.taskList.get(size - 1).getName());
assertTrue(TaskService.taskList.get(size - 1).getDescrip().equals("Update Name"));
}
//Test- update description
@Test
@DisplayName("Update Description")
void testTaskServiceUpdateDescrip() {
TaskService.addTask("Test 1", "testing description");
int size = TaskService.taskList.size();
System.out.println(TaskService.taskList.get(size - 1).getId());
System.out.println(TaskService.taskList.get(size - 1).getDescrip());
TaskService.updateDescrip("1000000002", "testing update description");
System.out.println(TaskService.taskList.get(size - 1).getDescrip());
assertTrue(TaskService.taskList.get(size - 1).getDescrip().equals("testing update description"));
}
// test- unique id
@Test
@DisplayName("Unique Id")
void testTaskServiceUniqueId() {
Task newTask = new Task("12345", "New Task", "Testing for unique Id");
TaskService.addTask(newTask);
Task duplicateId = new Task("12345", "New Task", "Testing for Id");
Assertions.assertThrows(IllegalArgumentException.class, () -> {
TaskService.addTask(duplicateId);
});
}
}
Any guidance is greatly welcomed
I've tried adding an order to my test in case the deletion test affected the other test and that didn't seem to work.
I'm working on a class assignment to which we are using junit to test our code. I have three test failing and I'm not sure how to fix it. I am not sure if it is the test that I have coded wrong or my methods. my task creation, updated name, and updated description are the ones that are failing. but my unique id & deletions work. Below are my methods
public class TaskService {
//variables
public static String taskId;
public String taskName;
public String taskDescrip;
//create an array for task list
static ArrayList<Task> taskList = new ArrayList<Task>(0);
//creates unique IDs
public static String generateUniqueId() {
String uniqueId;
//pulls the latest Id value in array as long as its not empty
if (taskList.isEmpty()) {
taskId = "1000000000";
}
else {
int arraySize = taskList.size();
taskId = taskList.get(arraySize - 1).getId();
}
// this will convert the id string into a integer to increment (making new id) then change it back to a string
int tempInt = Integer.valueOf(taskId);
tempInt += 1;
uniqueId = Integer.toString(tempInt);
return uniqueId;
}
//creates task
public static void addTask(String name, String descrip) {
String Id = generateUniqueId();
Task Task1 = new Task(Id, name, descrip);
taskList.add(Task1);
}
//add task
public static void addTask(Task newTask) {
String tempId = newTask.getId();
for (int i = 0; i < taskList.size(); i++) {
if (tempId.equals(taskList.get(i).getId())) {
throw new IllegalArgumentException("Id must be unique");
}
}
taskList.add(newTask);
}
//updates name
public static void updateName(String uniqueId, String taskName) {
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
taskList.get(i).setName(taskName);
}
}
}
//updates description
public static void updateDescrip(String uniqueId, String taskDescrip) {
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
taskList.get(i).setName(taskDescrip);
}
}
}
//deletes task
public static void deleteTask(String uniqueId) {
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
int position = i;
taskList.remove(position);
}
}
}
//search function for testing
public static int searchTask(String uniqueId) {
int result = 0;
for (int i = 0; i < taskList.size(); i++) {
if (uniqueIdpareTo(taskList.get(i).getId()) == 0) {
result = 1;
}
else {
result = 2;
}
}
return result;
}
}
and this is what I have written for the tests.
public class TaskServiceTest {
//Creation Test
@Test
@DisplayName("Task Creation")
void testTaskServiceClass() {
TaskService.addTask("Task Name", "Task Description");
assertTrue(TaskService.taskList.get(0).getId().equals("1000000001"));
assertTrue(TaskService.taskList.get(0).getName().equals("Task Name"));
assertTrue(TaskService.taskList.get(0).getDescrip().equals("Task Description"));
}
//Deletion Test
@Test
@DisplayName("Deletion Test")
void testTaskServiceDelete() {
TaskService.addTask("Task Service", "Testing Task Service");
int size = TaskService.taskList.size();
TaskService.deleteTask("1000000002");
assertTrue(TaskService.searchTask("1000000002") == 2);
}
//Test- update name
@Test
@DisplayName("Update name")
void testTaskServiceUpdateName() {
TaskService.addTask("Test 1", "Description");
int size = TaskService.taskList.size();
System.out.println(TaskService.taskList.get(size - 1).getId());
System.out.println(TaskService.taskList.get(size - 1).getName());
TaskService.updateName("1000000002", "Update Name");
System.out.println(TaskService.taskList.get(size - 1).getName());
assertTrue(TaskService.taskList.get(size - 1).getDescrip().equals("Update Name"));
}
//Test- update description
@Test
@DisplayName("Update Description")
void testTaskServiceUpdateDescrip() {
TaskService.addTask("Test 1", "testing description");
int size = TaskService.taskList.size();
System.out.println(TaskService.taskList.get(size - 1).getId());
System.out.println(TaskService.taskList.get(size - 1).getDescrip());
TaskService.updateDescrip("1000000002", "testing update description");
System.out.println(TaskService.taskList.get(size - 1).getDescrip());
assertTrue(TaskService.taskList.get(size - 1).getDescrip().equals("testing update description"));
}
// test- unique id
@Test
@DisplayName("Unique Id")
void testTaskServiceUniqueId() {
Task newTask = new Task("12345", "New Task", "Testing for unique Id");
TaskService.addTask(newTask);
Task duplicateId = new Task("12345", "New Task", "Testing for Id");
Assertions.assertThrows(IllegalArgumentException.class, () -> {
TaskService.addTask(duplicateId);
});
}
}
Any guidance is greatly welcomed
I've tried adding an order to my test in case the deletion test affected the other test and that didn't seem to work.
Share Improve this question asked Feb 2 at 3:39 KaitKait 11 silver badge 5 |1 Answer
Reset to default 3JUnit tests should be independent
JUnit chooses itself an order in which it runs your test methods. Which is good because it encourages us to write the tests in a way so they are independent of each other, which in turn makes it easier to read and analyse each test in isolation. Including figuring out what goes wrong in it if anything.
On my computer JUnit first runs testTaskServiceUniqueId()
, which passes as you said.
Next it runs testTaskServiceDelete()
. And your suspicion was correct. You wrote:
I've tried adding an order to my test in case the deletion test affected the other test and that didn't seem to work.
Indeed the Deletion Test
affects the following tests; but because although it passes, it doesn’t behave the way you intended. From testTaskServiceUniqueId()
there’s a task with ID 12345
. The test adds a new task, it gets ID 12346
. It then tries to delete a task with ID 1000000002
. There is no such task, so nothing happens. You might consider whether your delete method should return a boolean
indicating whether a task was in fact deleted (and in your unit test check that the expected Boolean value is returned from the method). Anyway, when you search for a task with ID 1000000002
, none is found and 2
is returned as expected.
The third test run is testTaskServiceUpdateName()
. It fails. It adds a task with ID 12347
. It then tries to update the name of a task with ID 1000000002
to Update Name
. A test with ID 1000000002
still doesn’t exist, so nothing happens. The test prints the name of the last task in the list, which remains Test 1
. It then asserts that this name should now be Update Name
and fails the test since it isn’t.
Here’s a way how you can write the test to be independent of what was in the task list already:
@Test
@DisplayName("Update name")
void testTaskServiceUpdateName() {
String nextUniqueId = TaskService.generateUniqueId();
Task newTask = new Task(nextUniqueId, "Test 1", "Description");
TaskService.addTask(newTask);
String updatedName = "Updated name";
TaskService.updateName(nextUniqueId, updatedName);
assertEquals(updatedName, newTask.getName());
}
I have left out the comment //Test- update name
since everyone can read the same information from the two annotations on the test. As Bjarne Stroutrup says, don’t put information in comments that you can state clearly in the programming language itself. I am creating the new task manually so I know its ID. And I hold on to the task so I can also use it for checking that the name is updated without having to access the task list in the service, which really would violate the encapsulation. I have introduced a variable holding the new updated name so I don’t need to repeat it (the DRY principle, don’t repeat yourself) and so I’m sure that I am testing for the right updated name in the end. Finally I use assertEquals()
rather than assertTrue()
. It’s clearer to read, and more importantly, it gives a much more telling failure message should the test fail, including both the expected name and the actual name.
Learn to use a debugger
I could not have answered this question without using a debugger. You too should learn how to use one, it will be immensely helpful.
本文标签: javaJunit test failingStack Overflow
版权声明:本文标题:java - Junit test failing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741856680a2401382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
taskName
andtaskDescrip
inTaskService
. Delete them. (2) UseassertEquals()
wherever you can. Most of yourassertTrue()
calls can be advantageously replaced withassertEquals()
. For clearer test code and not least far better failure messages. (3) Have you learned the enhanced for loop yet? It’s nifty.(4) IntestTaskServiceDelete()
you are not using the variablesize
. Delete it. – Ane De Jong Commented Feb 2 at 10:01assertTrue()
can — and should — be replaced with calls toassertEquals()
. (2) When the OP says my unique id & deletions work, I guess it means that the three failing tests aretestTaskServiceClass()
,testTaskServiceUpdateName()
andtestTaskServiceUpdateDescrip()
. – Anonymous Commented Feb 2 at 10:09