Question
I wrote one test case for one class. it compilers and shows 0 error but the test cases are just 0 pass and 3 failure
I wrote one test case for one class. it compilers and shows 0 error but the test cases are just 0 pass and 3 failure . Can anyone help me to fix it? I appreciate. Here's Original Class:
package synthpp; import processing.core.PApplet; import processing.core.PFont; import java.awt.Color; public class Button implements Clickable{ protected TextLabel text; protected PApplet pApplet; protected ButtonAdapter listener; protected boolean isPressed; protected boolean isDisabled; protected Color orginalForegroundColor;//used for mouseover effect protected Color orginalBackgroundColor; public Button(PApplet pApplet, String t, PFont pFont, int w, int h, int x, int y, Color bg, Color fg){ this.pApplet = pApplet; text = new TextLabel(pApplet,t, pFont, x,y,w,h,TextLabel.HALIGN.center, TextLabel.VALIGN.center,0); text.setBackgroundColor(bg); text.setForegroundColor(fg); this.orginalBackgroundColor = bg; this.orginalForegroundColor = fg; isPressed = false; isDisabled = false; } public void draw(){ text.draw(); } //these 2 are used for highlighting with mouseover effect public Color getOrginalForegroundColor(){ return this.orginalForegroundColor; } public Color getOrginalBackgroundColor(){ return orginalBackgroundColor; } public void setBackgroundColor(Color c){ text.setBackgroundColor(c); } public Color getBackgroundColor(){return text.getBackgroundColor();} public void setForgroundColor(Color c){ text.setForegroundColor(c); } public void addButtonListener(ButtonAdapter listener){ this.listener = listener; } @Override public boolean isPressed(){ return isPressed; } public boolean isDisabled(){ return isDisabled;} public void setDisabled(boolean d){ isDisabled = d;} @Override public void mousePressed(PApplet pApplet) { if(listener != null){ listener.mousePressed(pApplet); isPressed = true; } } @Override public void mouseReleased(PApplet pApplet) { if(listener != null && !isDisabled){ listener.mouseReleased(pApplet); isPressed = false; } } @Override public int getPosX() { return text.getPositionX(); } @Override public int getPosY() { return text.getPositionY(); } @Override public int getWidth() { return text.getWidth(); } @Override public int getHeight() { return text.getHeight(); } }
Here's the test cases i wrote:
import org.junit.Before; //JUnit 4 import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.Test; //JUnit 4
import processing.core.PApplet; import processing.core.PFont; import synthpp.Button; import synthpp.ButtonAdapter; import synthpp.TextLabel;
import java.awt.*; import java.io.File; import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
@Nested @DisplayName("Button Testing") public class ButtonTest { private static Button button; private Color orginalForegroundColor; private Color orginalBackgroundColor; private int x; private int y; private int w; private int h;
//Change to BeforeAll for JUnit 5 @Before public void initialize(PApplet pApplet, String t, PFont pFont, int w, int h, int x, int y, Color bg, Color fg) { this.orginalForegroundColor = fg; this.orginalBackgroundColor = bg; this.x = x; this.y = y; this.w = w; this.h = h; button = new Button(pApplet, t, pFont, w, h, x, y, bg, fg); }
@Test @DisplayName("get original foreground color") public void getOrginalForegroundColor() { assertEquals(orginalForegroundColor, button.getOrginalForegroundColor()); }
@Test @DisplayName("get original background color") public void getOrginalBackgroundColor() { assertEquals(orginalBackgroundColor, button.getOrginalBackgroundColor()); }
@Test @DisplayName("set background color") public void setBackgroundColor() { Color c = new Color(0, 0, 0); this.orginalBackgroundColor = c; button.setBackgroundColor(c); assertEquals(c, button.getBackgroundColor()); }
@Test @DisplayName("get background color") public void getBackgroundColor() { assertEquals(this.orginalBackgroundColor, button.getBackgroundColor()); }
@Test @DisplayName("set foreground color") public void setForgroundColor() { Color c = new Color(0, 0, 0); button.setForgroundColor(c); // Button class does not have getForegroundColor() method, how to test? // assertEquals(c, button.getForegroundColor()); }
@Test @DisplayName("mouse pressed") public void mousePressed(PApplet pApplet) { assertEquals(true, button.isPressed()); }
@Test @DisplayName("mouse released") public void mouseReleased(PApplet pApplet) { assertEquals(false, button.isPressed()); }
@Test @DisplayName("get position x") public void getPosX() { assertEquals(x, button.getPosX()); }
@Test @DisplayName("get position y") public void getPosY() { assertEquals(y, button.getPosY()); }
@Test @DisplayName("get width") public void getWidth() { assertEquals(w, button.getWidth()); }
@Test @DisplayName("get height") public void getHeight() { assertEquals(h, button.getHeight()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started