How do I test this with JUnit? I watched a video on YouTube about how to create and run a Simple JUnit test in Eclipse IDE but I still don't understand.
Here's what I need to test:
public class ReverseDomainName
{
public static void main(String[] args)
{
String domain =
"cs.princeton.edu";
System.out.println("domain is " +
domain);
System.out.println("reverse of
domain is " + rev_domain(domain));
}
public static String rev_domain(String domain)
{
String[] token =
domain.split("\\.");
String ans = "";
for(int i=token.length-1; i>=0;
i--)
{
if(i==0)
ans = ans + token[i];
else
ans = ans + token[i] + ".";
}
return ans;
}
}
And here's what I have after watching a tutorial video:
import static org.junit.Assert.*;
import org.junit.Test;
public class ReverseDomainNameTest
{
@Test
public void revTest()
{
ReverseDomainName junit = new
ReverseDomainName();
String result =
junit.rev_domain
}
}
JUNIT Test File to run:
import static org.junit.Assert.*;
import org.junit.Test;
public class ReverseDomainNameTest
{
@Test
public void revTest()
{
ReverseDomainName junit = new ReverseDomainName();
String domain="www.abc.com";
String result = junit.rev_domain(domain);
String result1 = junit.rev_domain("WWW.def.com");
assertEquals("com.abc.www",result);
assertEquals("com.def.WWW",result1);
}
}
if you like the answer please provide a thumbs up.
How do I test this with JUnit? I watched a video on YouTube about how to...
Given this JUnit test, write a SentenceCounter class that makes the tests pass: import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; /** * Tests that will make us practice writing loops */ public class TestSentenceCounter { private static final String SENTENCE1 = "This is my sentence."; private static final String SENTENCE2 = "These words make another sentence that is longer"; private SentenceCounter sc1; private SentenceCounter sc2; /** * Create two instances we can play with */ @Before public void setup()...
How can I write a unit test for the following java method? (I am using intelliJ) public class ReviewEmployee{ public void EmployeeReview(Employee currentEmployee){ System.out.println("Your recorded hours for the week are: ") + (currentEmployee.getHours()); System.out.println("Your satisfactory rating is: ") + (currentlyEmployee.getRating()); System.out.println("Your bonus is: "); if(currentEmployee.getRating() < 3){ System.out.println(currentlyEmployee.getBonus()); } else{ System.out.println(currentlyEmployee.getBonus() + 500); } } } The Employee class is as follows: Public class Employee{ Private double hours; Private double rating; Private double bonus; Public Employee(double...
In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); } public static String printBackwards(String one)...
Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAnswer{ private String question; public Essay(String q){ this.question = q; } //This function returns question text public String getQuestionText() { return question; } //This function takes answer from user public void answer(String userAnswer) { // Take care of answer } @Override public String getAnswer() { System.out.println(question); Scanner scan = new Scanner(System.in); System.out.print("Answer: "); String ans =scan.nextLine(); scan.close(); if(ans.length() <=140){ return ans; }else{ return...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...
Junit testing case Here are my methods I want to test. Scanner scan = new Scanner(System.in).useDelimiter("\n"); public String prompt_FirstName() { System.out.println("First Name:"); String firstName = scan.next(); return firstName; } Here's what i have so far @Test public void testPrompt_first() { Menu inputOutput= new Menu(); String input = "Jane"; InputStream in = new ByteArrayInputStream(input.getBytes()); System.setIn(in); String s = inputOutput.prompt_FirstName(); assertEquals("Jane", s); }
(How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...
Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies a utility method to reverse the entries in a linked list. Then, test your code using the tester class, Reverse Tester.java, given below. ListUtil.java import java.util.LinkedList; /** This class supplies a utility method to reverse the entries in a linked list. */ public class ListUtil { /** Reverses the elements in a linked list @param strings the linked list to reverse public static void...
How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...
# Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a button and place it in the scene Button btOK = new Button("OK"); btOK.setOnAction(e -> System.out.println("OK 1")); btOK.setOnAction(e -> System.out.println("OK 2")); Scene scene = new Scene(btOK, 200, 250); primaryStage.setTitle("MyJavaFX"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage...