@@ -1308,7 +1308,7 @@ jUnit Tests
1308
1308
public void testMain() throws IOException
1309
1309
{
1310
1310
// I wrote a method to run a method and send back the output - only works with String[] args for now
1311
- String output = getMethodOutput("StudentCode", " main");
1311
+ String output = getMethodOutput("main");
1312
1312
String expect = "2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n 10\n ";
1313
1313
1314
1314
assertEquals("Output doesn't match", cleanString(expect), cleanString(output));
@@ -1318,7 +1318,7 @@ jUnit Tests
1318
1318
@Test
1319
1319
public void testAdder() throws IOException {
1320
1320
StudentCode s = new StudentCode();
1321
- String msg = createMessage( "Adding 2+2", ""+4, ""+s.adder(2,2) );
1321
+ String msg = "Adding 2+2" + ""+4 + ""+s.adder(2,2);
1322
1322
System.out.println("testing s.adder(2,2)");
1323
1323
assertEquals(msg, 4, s.adder(2,2));
1324
1324
assertEquals("adding 3+3", 6, s.adder(3,3));
@@ -1380,7 +1380,7 @@ jUnit Tests
1380
1380
public void testMain() throws IOException
1381
1381
{
1382
1382
// I wrote a method to run a method and send back the output - only works with String[] args for now
1383
- String output = getMethodOutput("StudentCode", " main");
1383
+ String output = getMethodOutput("main");
1384
1384
String expect = "2\n3\n4\n5\n6\n7\n8\n9\n10\n";
1385
1385
1386
1386
assertEquals("Output doesn't match", cleanString(expect), cleanString (output));
@@ -1403,6 +1403,154 @@ jUnit Tests
1403
1403
}
1404
1404
}
1405
1405
1406
+ .. activecode :: jUnitTesting3
1407
+ :language: java
1408
+ :autograde: unittest
1409
+
1410
+ In this project, you will create a class that can tell riddles like the following:
1411
+
1412
+ Riddle Question: Why did the chicken cross the playground?
1413
+ Riddle Answer: To get to the other slide!
1414
+ ~~~~
1415
+ public class Riddle
1416
+ {
1417
+ // 2 instance variables for Riddle's question and answer: private type variableName;
1418
+ private String quest;
1419
+ private String ans;
1420
+
1421
+ // constructor
1422
+ public Riddle(String initQuestion, String initAnswer)
1423
+ {
1424
+ // set the instance variables to the init parameter variables
1425
+ quest = initQuestion;
1426
+ ans = initAnswer;
1427
+ }
1428
+
1429
+ // Print riddle question
1430
+ public void printQuestion()
1431
+ {
1432
+ // print out the riddle question with System.out.println
1433
+ System.out.println(quest);
1434
+ }
1435
+
1436
+ // Print riddle answer
1437
+ public void printAnswer()
1438
+ {
1439
+ // print out the riddle answer with System.out.println
1440
+ System.out.println(ans);
1441
+ }
1442
+
1443
+ // main method for testing
1444
+ public static void main(String[] args)
1445
+ {
1446
+ // call the constructor to create 3 new Riddle objects
1447
+
1448
+ // call their printQuestion() and printAnswer methods
1449
+
1450
+ }
1451
+ }
1452
+ ====
1453
+ //import codetesthelper.*;
1454
+
1455
+ // Test Code for Lesson 5.1.5 - Riddle
1456
+ import static org.junit.Assert.*;
1457
+ import org.junit.After;
1458
+ import org.junit.Before;
1459
+ import org.junit.Test;
1460
+
1461
+ import java.io.*;
1462
+
1463
+ public class RunestoneTests extends CodeTestHelper
1464
+ {
1465
+ public RunestoneTests()
1466
+ {
1467
+ super("Riddle"); // class name / location of main
1468
+
1469
+ Object[] values = new Object[]{"Question", "Answer"};
1470
+ setDefaultValues(values);
1471
+ }
1472
+
1473
+ @Test
1474
+ public void testPrintQuestion()
1475
+ {
1476
+ String output = getMethodOutput("printQuestion");
1477
+ String expect = "Question";
1478
+
1479
+ boolean passed = getResults(expect, output, "Checking method printQuestion()");
1480
+ assertTrue(passed);
1481
+ }
1482
+
1483
+ @Test
1484
+ public void testPrintAnswer()
1485
+ {
1486
+ String output = getMethodOutput("printAnswer");
1487
+ String expect = "Answer";
1488
+
1489
+ boolean passed = getResults(expect, output, "Checking method printAnswer()");
1490
+ assertTrue(passed);
1491
+ }
1492
+
1493
+ @Test
1494
+ public void testDefaultConstructor()
1495
+ {
1496
+ String[] args = {"Question 1", "Answer 1"};
1497
+ String output = checkDefaultConstructor();
1498
+ String expect = "fail";
1499
+
1500
+ boolean passed = getResults(expect, output, "Checking default constructor");
1501
+ assertTrue(passed);
1502
+ }
1503
+
1504
+ @Test
1505
+ public void testConstructor()
1506
+ {
1507
+ String[] args = {"Question 1", "Answer 1"};
1508
+ String output = checkConstructor(args);
1509
+ String expect = "pass";
1510
+
1511
+ boolean passed = getResults(expect, output, "Checking constructor with parameters");
1512
+ assertTrue(passed);
1513
+ }
1514
+
1515
+ @Test
1516
+ public void testVariableTypes()
1517
+ {
1518
+ String varTypes = "String String";
1519
+ String output = testInstanceVariableTypes(varTypes.split(" "));
1520
+
1521
+ boolean passed = getResults(varTypes, output, "Checking Instance Variable Type(s)");
1522
+ assertTrue(passed);
1523
+ }
1524
+
1525
+ @Test
1526
+ public void testPrivateVariables()
1527
+ {
1528
+ String expect = "2 Private";
1529
+ String output = testPrivateInstanceVariables();
1530
+
1531
+ boolean passed = getResults(expect, output, "Checking Private Instance Variable(s)");
1532
+ assertTrue(passed);
1533
+ }
1534
+
1535
+
1536
+ @Test
1537
+ public void testMain()
1538
+ {
1539
+ String output = getMethodOutput("main");
1540
+
1541
+ String expect = "6 line(s) of text";
1542
+ String actual = " line(s) of text";
1543
+
1544
+ if (output.length() > 0) {
1545
+ actual = output.split("\n ").length + actual;
1546
+ } else {
1547
+ actual = output.length() + actual;
1548
+ }
1549
+ boolean passed = getResults(expect, actual, "Checking main method");
1550
+ assertTrue(passed);
1551
+ }
1552
+ }
1553
+
1406
1554
Catch2 C++ Unit Tests
1407
1555
---------------------
1408
1556
0 commit comments