Question
I tried to write test cases for one class. it compilers and shows 0 error but the test cases are just 0 pass and 3
I tried to write test cases 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. If you fix or change please note it .
Here's test cases class i wrote:
package synthpp;
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()); } }
Here's are the original class and some support classes:
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 support class clickable:
package synthpp; import processing.core.PApplet; public interface Clickable { public void mousePressed(PApplet pApplet); public void mouseReleased(PApplet pApplet); public int getPosX(); public int getPosY(); public int getWidth(); public int getHeight(); public boolean isPressed(); public static boolean isMouseOver(PApplet pApplet, Clickable clickable){ if (pApplet.mouseX >= clickable.getPosX() && pApplet.mouseX <= clickable.getPosX()+ clickable.getWidth() && pApplet.mouseY >= clickable.getPosY() && pApplet.mouseY <= clickable.getPosY()+clickable.getHeight()) { return true; } else { return false; } } }
Here's support TextLabel class:
package synthpp; import processing.core.*; import java.awt.Color; import java.awt.Toolkit; public class TextLabel { //PApplet window we will draw into PApplet pApplet; //text on the label private String text; private PFont pFont; //geometry in MainWindow private int positionX; private int positionY; private int positionZ; private int width; private int height; //foreground/background colors private Color backgroundColor; private Color foregroundColor; //border color private Color strokeColor; private float radius; //alignment HALIGN hAlign; VALIGN vAlign; int minLeftMargin; public static enum HALIGN{ left, center, right } public static enum VALIGN{ top, center, bottom } public TextLabel(PApplet pApplet, String text, PFont font, int x, int y, int w, int h, HALIGN hAlign, VALIGN vAlign, int minLeftMargin) { this.pApplet = pApplet; this.setText(text); this.pFont = font; positionX = x; positionY = y; this.hAlign = hAlign; this.vAlign = vAlign; this.minLeftMargin = minLeftMargin; double textWidth = setTextWidth(text); width = (int)(textWidth > w ? textWidth:w); double fontHeight = setFontHeight(); height = (int)(fontHeight > h ? fontHeight:h); backgroundColor = Color.black; foregroundColor = Color.white; strokeColor = new Color(100,100,100, 0); } //setter for the textWdith public double setTextWidth(String text) { double textWidth; if(!text.isEmpty()) { //textWidth = pApplet.textWidth(text); textWidth = (double) text.length(); } else { textWidth = 0.0; } return textWidth; } public double setFontHeight() { if(pFont != null) { return pFont.getSize() * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0; } else { return 0.0; } } void draw(){ //width = (int)((pApplet.textWidth(text) > width)?pApplet.textWidth(text):width); pApplet.stroke(strokeColor.getRGB(),strokeColor.getAlpha()); pApplet.fill(backgroundColor.getRGB()); pApplet.rect(positionX, positionY, width, height); pApplet.textFont(pFont); pApplet.fill(foregroundColor.getRGB()); double fontSize = pFont.getSize() * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0; int drawX = positionX; int drawY = positionY; if(hAlign == HALIGN.center) { int textX = (positionX) + (width / 2) - ((int) (pApplet.textWidth(text) / 2)); int textY = (positionY + (height / 2) + ((int) (fontSize) / 4)); drawX = textX > positionX ? textX : positionX; drawY = textY > positionY ? textY : positionY; }else if(hAlign == HALIGN.left){ int textY = (positionY + (height/2) + ((int)(fontSize)/4)); drawX = positionX + minLeftMargin; //we have to add minLeftMargin because left margin will show up here drawY = textY > positionY?textY:positionY; }else if(hAlign == HALIGN.right){ int textX = (positionX) + (width / 2) - ((int) (pApplet.textWidth(text) / 2)); int textY = (positionY + (height / 2) + ((int) (fontSize) / 4)); drawX = textX > positionX ? textX : positionX; drawY = textY > positionY ? textY : positionY; } pApplet.text(text,drawX,drawY); } public String getText() { return text; } public void setText(String text) { this.text = text; } public PFont getpFont() { return pFont; } public void setpFont(PFont pFont) { this.pFont = pFont; } public int getPositionX() { return positionX; } public void setPositionX(int positionX) { this.positionX = positionX; } public int getPositionY() { return positionY; } public void setPositionY(int positionY) { this.positionY = positionY; } public int getPositionZ() { return positionZ; } public void setPositionZ(int positionZ) { this.positionZ = positionZ; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public Color getBackgroundColor() { return backgroundColor; } public void setBackgroundColor(Color backgroundColor) { this.backgroundColor = backgroundColor; } public Color getForegroundColor() { return foregroundColor; } public void setForegroundColor(Color foregroundColor) { this.foregroundColor = foregroundColor; } }
Here's support Button adapter class:
package synthpp; import processing.core.PApplet; public abstract class ButtonAdapter{ public abstract void mousePressed(PApplet pApplet); public abstract void mouseReleased(PApplet pApplet); }
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