so I'm currently learning on how to do better test cases on JUnit. So, I'm trying to test for an exception on my addItem() method, but the test keeps failing, and I don't know if maybe it has something to do with the model's if statement or maybe I'm just using the assertException() wrong
Here is the model code:
package com.csis3275.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MiniBar {
private int itemCapacity;
private String roomNo;
private List<MiniBarItem> items = new ArrayList<>();
public MiniBar(String assignedRoomNo, int newBarCapacity) {
this.itemCapacity = newBarCapacity;
this.roomNo = assignedRoomNo;
}
//Add an item
public void addItem(MiniBarItem newItem) {
try {
//Check the capacity
if (items.size() == this.itemCapacity) {
throw new Exception("The bar is full!");
} else {
//Add the item
items.add(newItem);
}
} catch (Exception barFull) {
System.out.println(barFull.getMessage());
}
}
//Remove and Item
public MiniBarItem removeItem(MiniBarItem itemToRemove) {
MiniBarItem itemToReturn = new MiniBarItem();
try {
if (items.size() != 0) {
for (MiniBarItem barItem : this.items) {
if (itemToRemove.getName() == barItem.getName()) {
return barItem;
}
}
} else {
throw new Exception("The bar is empty!");
}
} catch (Exception barEmpty) {
System.out.println(barEmpty.getMessage());
}
return null;
}
//List Items that are expired
public List<MiniBarItem> expiredItems (){
List<MiniBarItem> expiredItems = new ArrayList<>();
Date currentDate = new Date();
System.out.print(currentDate);
for (MiniBarItem item : this.items) {
if (item.getBestBeforeDate().after(currentDate) ) {
expiredItems.add(item);
System.out.println("Added item: " + item);
}
else {
System.out.println("No expired Items on list");
}
}
return expiredItems;
}
//Produce a bill
}
And here is the test
class MiniBarIntegrationTest {
MiniBar myBar;
List<MiniBarItem> testItems;
@BeforeEach()
void init(){
myBar = new MiniBar("208", 12 );
testItems = new ArrayList<>();
testItems.add(new MiniBarItem("Cookie", 19, new Date(2020, 03, 12)));
testItems.add(new MiniBarItem("Water", 20, new Date(2021, 03, 12)));
testItems.add(new MiniBarItem("Coke", 21, new Date(2020, 12, 12)));
testItems.add(new MiniBarItem("Munchies", 23, new Date(2022, 03, 12)));
testItems.add(new MiniBarItem("Weed", 27, new Date(2019, 03, 12)));
testItems.add(new MiniBarItem("Beer", 19, new Date(2020, 11, 12)));
for(MiniBarItem b : this.testItems ) {
myBar.addItem(b);
}
}
@Test
void testAddItemsToFullList() {
List<MiniBarItem> testItems1 = new ArrayList<>();
testItems1.add(new MiniBarItem("6", 19, new Date(2020, 03, 12)));
testItems1.add(new MiniBarItem("7", 20, new Date(2021, 03, 12)));
testItems1.add(new MiniBarItem("8", 21, new Date(2020, 12, 12)));
testItems1.add(new MiniBarItem("9", 21, new Date(2020, 12, 12)));
testItems1.add(new MiniBarItem("10", 21, new Date(2020, 12, 12)));
testItems1.add(new MiniBarItem("11", 21, new Date(2020, 12, 12)));
for(MiniBarItem b :testItems1 ) {
myBar.addItem(b);
}
assertThrows(Exception.class,
()->myBar.addItem(new MiniBarItem("Weed", 27, new Date(2019, 03, 12))));
}
}
I'm getting this error on JUnit
java.lang.AssertionError: expected java.lang.Exception to be thrown, but nothing was thrown
at org.junit.Assert.assertThrows(Assert.java:1028)
at org.junit.Assert.assertThrows(Assert.java:981)
at com.csis3275.test.model.MiniBarIntegrationTest.testAddItemsToFullList(MiniBarIntegrationTest.java:82)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:205)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:201)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Aucun commentaire:
Enregistrer un commentaire