I am trying to perform what I considered to be a very basic JUnit test that includes some parameters, but for some reason the test doesn't fail nor pass but simply generates a rather convoluted error message which, to someone with little understanding of JUnit testing, is difficult to comprehend. I have searched online for information regarding this problem and apparently it's related to parameterized testing but despite attempting a a couple of fix's the test still wont run.
How on earth can I just get this JUnit test to actually run? I feel like I am missing something obvious but I haven't got a clue what that is.
I have included my code, including the JUnit test in question, below -> Main class boids ->
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import static java.lang.Math.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import javax.swing.Timer;
import com.sun.javafx.font.directwrite.RECT;
import java.util.Random;
import javax.swing.JButton;
public class Boids extends JPanel {
private static final int RECT_X = 400;
private static final int RECT_Y = RECT_X;
private static final int RECT_WIDTH = 100;
private static final int RECT_HEIGHT = RECT_WIDTH;
Flock flock;
Flock flock2;
final int w, h;
int noSuccessful;
public Boids() {
w = 1200;
h = 600;
setPreferredSize(new Dimension(w, h));
setBackground(Color.black);
spawnFlock();
spawnFlock2();
// checkFood();
// flock.hasFoundFood(noSuccessful);
// System.out.print(flock.hasFoundFood(noSuccessful));
checkFood();
new Timer(17, (ActionEvent e) -> {
if (flock.hasLeftTheBuilding(w))
spawnFlock();
// checkFood();
repaint();
// System.out.println();
}).start();
new Timer(18, (ActionEvent e) -> {
if (flock2.hasLeftTheBuilding(w) && (flock.hasLeftTheBuilding(w)))
spawnFlock2();
repaint();
}).start();
}
// flock.hasFoundFood(noSuccessful);
/* new Timer(1, (ActionEvent e) -> {
if (flock.hasLeftTheBuilding(w))
flock.hasFoundFood(noSuccessful);
}).restart();
*/
/* new Timer(17, (ActionEvent e) -> {
if (flock.hasLeftTheBuilding(w))
// spawnFlock();
// checkFood();
// repaint();
System.out.println(flock.hasFoundFood(noSuccessful));
}).start();
} */
public void checkFood() {
// System.out.println(flock.hasFoundFood(noSuccessful));
}
public void spawnFlock() {
Random rand = new Random();
int n = rand.nextInt(599) + 1;
//implement random for width as well as height
flock = Flock.spawn(100, h - n, 20);
flock2 = Flock.spawn(100, h - n, 1);
}
public void spawnFlock2() {
Random rand = new Random();
int n = rand.nextInt(599) + 1;
//implement random for width as well as height
// flock2 = Flock.spawn(100, h - n, 1);
}
@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// SimGUI buttonx = new SimGUI();
/* buttonx.btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event1) {
flock.run(g, w, h);
}
});
*/
flock.run(g, w, h);
// flock2.run(g, w, h);
// flock.hasFoundFood(noSuccessful);
// super.paintComponent(g);
// draw the rectangle here
// gg.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
// ((Graphics2D) gg).draw(new Rectangle2D.Double(400,100,100,50));
Shape rect = new Rectangle2D.Double(190, 190, 200, 200);
g.draw(rect);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Simulator v0.6");
f.setResizable(false);
f.add(new Boids(), BorderLayout.CENTER);
// f.add(new Boids(), BorderLayout.CENTER); to add another flock to environment
//leaders will also be added in this fashion
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
// SimGUI.getFrames();
// SimGUI test = new SimGUI();
// test.panel.setVisible(true);
SimGUI test = new SimGUI();
f.getContentPane().add(test, BorderLayout.EAST); //just positions a slither to the right, dont understand why it wont display jpanel -->
//COULD THIS BE BECAUSE OF ME SETTING PREFERRED SIZE AND WIDTH/HEIGHT OUTSIDE OF MAIN? SEE "public Boids"...
//SimGUI tempt = new SimGUI();
// tempt.setVisible(true);
});
}
// private void printPanelCompPoints(JPanel f) {
// f.getComponentAt(600, 500);
//currently working on this = voting system
//see psuedocode!
}
//}
class Boid {
static final Random r = new Random();
static final Vec migrate = new Vec(0.02, 0);
static final int size = 3;
static final Path2D shape = new Path2D.Double();
static {
shape.moveTo(0, -size * 2);
shape.lineTo(-size, size * 2);
shape.lineTo(size, size * 2);
shape.closePath();
}
final double maxForce, maxSpeed;
Vec location, velocity, acceleration;
private boolean included = true;
Boid(double x, double y) {
acceleration = new Vec();
velocity = new Vec(r.nextInt(3) + 1, r.nextInt(3) - 1);
location = new Vec(x, y);
maxSpeed = 3.0;
maxForce = 0.05;
}
void update() {
velocity.add(acceleration);
velocity.limit(maxSpeed);
location.add(velocity);
acceleration.mult(0);
}
void applyForce(Vec force) {
acceleration.add(force);
}
Vec seek(Vec target) {
Vec steer = Vec.sub(target, location);
steer.normalize();
steer.mult(maxSpeed);
steer.sub(velocity);
steer.limit(maxForce);
return steer;
}
void flock(Graphics2D g, List<Boid> boids) {
view(g, boids);
Vec rule1 = separation(boids);
Vec rule2 = alignment(boids);
Vec rule3 = cohesion(boids);
rule1.mult(2.5);
rule2.mult(1.5);
rule3.mult(1.3);
applyForce(rule1);
applyForce(rule2);
applyForce(rule3);
applyForce(migrate);
}
void view(Graphics2D g, List<Boid> boids) {
double sightDistance = 100;
double peripheryAngle = PI * 0.85;
for (Boid b : boids) {
b.included = false;
if (b == this)
continue;
double d = Vec.dist(location, b.location);
if (d <= 0 || d > sightDistance)
continue;
Vec lineOfSight = Vec.sub(b.location, location);
double angle = Vec.angleBetween(lineOfSight, velocity);
if (angle < peripheryAngle)
b.included = true;
}
}
Vec separation(List<Boid> boids) {
double desiredSeparation = 25;
Vec steer = new Vec(0, 0);
int count = 0;
for (Boid b : boids) {
if (!b.included)
continue;
double d = Vec.dist(location, b.location);
if ((d > 0) && (d < desiredSeparation)) {
Vec diff = Vec.sub(location, b.location);
diff.normalize();
diff.div(d); // weight by distance
steer.add(diff);
count++;
}
}
if (count > 0) {
steer.div(count);
}
if (steer.mag() > 0) {
steer.normalize();
steer.mult(maxSpeed);
steer.sub(velocity);
steer.limit(maxForce);
return steer;
}
return new Vec(0, 0);
}
Vec alignment(List<Boid> boids) {
double preferredDist = 50;
Vec steer = new Vec(0, 0);
int count = 0;
for (Boid b : boids) {
if (!b.included)
continue;
double d = Vec.dist(location, b.location);
if ((d > 0) && (d < preferredDist)) {
steer.add(b.velocity);
count++;
}
}
if (count > 0) {
steer.div(count);
steer.normalize();
steer.mult(maxSpeed);
steer.sub(velocity);
steer.limit(maxForce);
}
return steer;
}
Vec cohesion(List<Boid> boids) {
double preferredDist = 50;
Vec target = new Vec(0, 0);
int count = 0;
for (Boid b : boids) {
if (!b.included)
continue;
double d = Vec.dist(location, b.location);
if ((d > 0) && (d < preferredDist)) {
target.add(b.location);
count++;
}
}
if (count > 0) {
target.div(count);
return seek(target);
}
return target;
}
/* Vec avoid(List<Boid> boids) {
int up = 0;
int down = 0;
for (Boid b : boids) {
if (b.location.x + 100 > 600 ) {
up = 1;
}
}
return velocity = new Vec (0,1);
} */
void draw(Graphics2D g) {
AffineTransform save = g.getTransform();
g.translate(location.x, location.y);
g.rotate(velocity.heading() + PI / 2);
g.setColor(Color.green);
g.fill(shape);
g.setColor(Color.green);
g.draw(shape);
g.setTransform(save);
// g.drawRect(600, 400, 100, 100);
}
public void run(Graphics2D g, List<Boid> boids, int w, int h) { //similair method to run leader
flock(g, boids);
update();
draw(g);
//may need additional run method for leader
}
}
class Flock {
List<Boid> boids;
Flock() {
boids = new ArrayList<>();
}
void run(Graphics2D g, int w, int h) {
for (Boid b : boids) {
b.run(g, boids, w, h);
}
}
boolean hasLeftTheBuilding(int w) {
int count = 0;
for (Boid b : boids) {
if (b.location.x + Boid.size > w) //will also be used to calculate votes based on whether boids is near food
count++;
}
return boids.size() == count;
}
void addBoid(Boid b) {
boids.add(b);
}
static Flock spawn(double w, double h, int numBoids) {
Flock flock = new Flock();
for (int i = 0; i < numBoids; i++)
flock.addBoid(new Boid(w, h));
return flock;
}
int hasFoundFood(int noSuccessful, Rectangle2D rect) {
noSuccessful = 0;
// for (Boid b : boids) {
if ( Boid.shape.intersects(rect.getBounds()) == true);
noSuccessful++;
// b.shape.getBounds2D().intersectsLine(400, 500, 400, 500);
// System.out.println(b.location);
return noSuccessful;
}
}
class Vec {
double x, y;
Vec() {
}
Vec(double x, double y) {
this.x = x;
this.y = y;
}
void add(Vec v) {
x += v.x;
y += v.y;
}
void sub(Vec v) {
x -= v.x;
y -= v.y;
}
void div(double val) {
x /= val;
y /= val;
}
void mult(double val) {
x *= val;
y *= val;
}
double mag() {
return sqrt(pow(x, 2) + pow(y, 2));
}
double dot(Vec v) {
return x * v.x + y * v.y;
}
void normalize() {
double mag = mag();
if (mag != 0) {
x /= mag;
y /= mag;
}
}
void limit(double lim) {
double mag = mag();
if (mag != 0 && mag > lim) {
x *= lim / mag;
y *= lim / mag;
}
}
double heading() {
return atan2(y, x);
}
static Vec sub(Vec v, Vec v2) {
return new Vec(v.x - v2.x, v.y - v2.y);
}
static double dist(Vec v, Vec v2) {
return sqrt(pow(v.x - v2.x, 2) + pow(v.y - v2.y, 2));
}
static double angleBetween(Vec v, Vec v2) {
return acos(v.dot(v2) / (v.mag() * v2.mag()));
}
}
so that the program can be run, here is the SimGUI class
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JToolBar;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Color;
import com.jgoodies.forms.layout.FormSpecs;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
public class SimGUI extends JPanel {
/**
* Create the panel.
*/
//JPanel panel = new JPanel();
public JPanel panel_1 = new JPanel();
private JTextField textField;
private JTextField textField_1;
public JButton btnRun = new JButton("Run");
public SimGUI() {
JSlider sizeOfFlock = new JSlider();
sizeOfFlock.setValue(10);
sizeOfFlock.setMaximum(20);
sizeOfFlock.setMinimum(1);
JLabel lblNewLabel = new JLabel("Settings");
lblNewLabel.setFont(new Font("Sitka Banner", Font.BOLD, 18));
JLabel lblFlockSize = new JLabel("Flock Size");
lblFlockSize.setFont(new Font("Sitka Heading", Font.BOLD, 16));
JLabel label = new JLabel("1");
label.setFont(new Font("Sitka Display", Font.PLAIN, 14));
JLabel label_1 = new JLabel("20");
label_1.setFont(new Font("Sitka Display", Font.PLAIN, 14));
JLabel lblTypesOfLeader = new JLabel("Types of Leader:");
lblTypesOfLeader.setFont(new Font("Sitka Heading", Font.BOLD, 16));
JLabel lblNewLabel_1 = new JLabel("Spawn a leader inside the Flock");
lblNewLabel_1.setFont(new Font("Sitka Subheading", Font.PLAIN, 14));
JLabel lblSpawnAPersistant = new JLabel("Spawn a persistant leader");
lblSpawnAPersistant.setFont(new Font("Sitka Subheading", Font.PLAIN, 14));
JCheckBox checkBox = new JCheckBox("");
JCheckBox checkBox_1 = new JCheckBox("");
JSlider noOfRuns = new JSlider();
textField = new JTextField();
textField.setColumns(10);
textField.setText("" + noOfRuns.getValue());
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setText("" + sizeOfFlock.getValue());
/**
* Handle change event for sizeOfFlock slider.
*/
sizeOfFlock.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
textField_1.setText(String.valueOf(sizeOfFlock.getValue()));
}
});
textField_1.addKeyListener(new KeyAdapter(){
@Override
public void keyReleased(KeyEvent ke) {
String typed = textField_1.getText();
sizeOfFlock.setValue(0);
if(!typed.matches("\\d+") || typed.length() > 3) {
return;
}
int value = Integer.parseInt(typed);
sizeOfFlock.setValue(value);
}
});
/**
* Handle change event for noOfRuns slider.
*/
noOfRuns.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
textField.setText(String.valueOf(noOfRuns.getValue()));
}
});
textField.addKeyListener(new KeyAdapter(){
@Override
public void keyReleased(KeyEvent ke) {
String typed = textField.getText();
noOfRuns.setValue(0);
if(!typed.matches("\\d+") || typed.length() > 3) {
return;
}
int value = Integer.parseInt(typed);
noOfRuns.setValue(value);
}
});
JLabel label_2 = new JLabel("1");
label_2.setFont(new Font("Sitka Display", Font.PLAIN, 14));
JLabel label_3 = new JLabel("100");
label_3.setFont(new Font("Sitka Display", Font.PLAIN, 14));
JLabel lblNoOfSimulation = new JLabel("No. of runs");
lblNoOfSimulation.setFont(new Font("Sitka Heading", Font.BOLD, 16));
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(83)
.addComponent(lblNewLabel)
.addGap(82)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(label, GroupLayout.PREFERRED_SIZE, 11, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(sizeOfFlock, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblFlockSize)))
.addContainerGap())
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblNoOfSimulation)
.addContainerGap(207, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 11, GroupLayout.PREFERRED_SIZE)
.addGap(2)
.addComponent(noOfRuns, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
.addGap(6))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblTypesOfLeader)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(lblNewLabel_1)
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(checkBox_1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
.addComponent(checkBox)))
.addComponent(lblSpawnAPersistant, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))
.addContainerGap(53, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(86)
.addComponent(btnRun, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
.addContainerGap(136, Short.MAX_VALUE))
);
btnRun.addActionListener(new ActionListener() { //CURRENTLY NOT WORKING
public void actionPerformed(ActionEvent e) {
Boids button1 = new Boids();
}
});
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(5)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(5)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblNewLabel)))
.addPreferredGap(ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
.addComponent(lblFlockSize)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addComponent(sizeOfFlock, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(28)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(lblTypesOfLeader)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblNewLabel_1))
.addComponent(checkBox))
.addGap(11)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSpawnAPersistant, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addComponent(checkBox_1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
.addGap(30)
.addComponent(lblNoOfSimulation, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(noOfRuns, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(18)
.addComponent(btnRun, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
.addGap(225))
);
setLayout(groupLayout);
};
here is the JUnit class ->
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
class spawnFlockTest {
@Test
void test(double h, double w, Object flock) {
Boids test = new Boids();
test.flock = Flock.spawn(w, h, 20 );
assertNotNull(test.flock);
}
}
}
In the process of trying to fix this problem some extra imports were added but they have made no difference, the error still crops up. Any help with this issue would be greatly appreciated as I need to test the entire code and it's extremely frustrating that I can even perform a single test so far
Aucun commentaire:
Enregistrer un commentaire