I want to test this method in a junit and one more.
Asked
Active
Viewed 76 times
0
-
You typed `fahrenheit` instead of `Fahrenheit` in your `test()` method. Java is case sensitive. – pafau k. May 31 '20 at 20:51
-
where is your method? – TongChen Jun 04 '20 at 00:37
2 Answers
0
You are trying to instantiate an object of class fahrenheit in your test class, then since you dont have a class named fahrenheit you are getting error.

Arif Akkas
- 54
- 6
0
I think you want a new instance of Temperature
, which should look like this: Temperature temperature = new Temperature();
. You can use this to set the internal fahrenheit value of 30 temperature.setFahrenheit(30);
and get the celcius value by double celsius = temperature.toCelsius();

Benjamin Eckardt
- 709
- 6
- 10
-
I can't do temperature = new Temperature(30);. I can only do temperature = new Temperature(); @Test public void test() { Temperature temperature = new Temperature(); double Temperature = 30; double celsius = temperature.toCelsius(); } } This is what I did now, but I feel like I'm doing it wrong – Dave Young May 31 '20 at 22:09
-
-
Anyway there are some things that could improve your solution. Passing an initial value via constructor or using static factory methods https://stackoverflow.com/questions/38561736/joshua-bloch-item-1-consider-static-factory-methods-instead-of-constructors . This would enable you to do something like `Temperature.fromFahrenheit(30)` or `Temperature.fromCelsius(50)`. – Benjamin Eckardt May 31 '20 at 22:41
-
Additionally `toFahrenheit(double tempC)` and `toCelsius(double tempF)` could always be replaced by their parameterless implementations and vice versa. Would that reduce your feeling of doing something wrong? – Benjamin Eckardt May 31 '20 at 22:44
-
I thought I could do this after, Temperature expected = new Temperature(86); But I'm getting an error now, the same as when I put 30 in parentheses – Dave Young May 31 '20 at 22:50
-
Did you add the constructor? `public class Temperature { private double fahrenheit = 0; /* Constructor*/ public Temperature(final double fahrenheit) { this.fahrenheit = fahrenheit; }`. Sorry, multiline code looks messy. – Benjamin Eckardt May 31 '20 at 23:01
-
-
After I input double celsius = temp.toCelsius(); Since I'm using a Junit test, how would I make sure that the celsius value is equal to 86 degrees from 30 degrees Fahrenheit using a unit? This is all of my code so far. public class Testing { @Test public void test() { Temperature temp = new Temperature(30); double celsius = temp.toCelsius(); Temperature expected = new Temperature(86); } – Dave Young Jun 01 '20 at 00:52
-
What about this? @Test public void test() { Temperature temp = new Temperature(30); double Temperature = temp.toCelsius(); Temperature expected = new Temperature(86); assertEquals(expected, temp); } – Dave Young Jun 01 '20 at 01:11
-
More something like this: `@Test public void test() { Temperature temp = new Temperature(30); double actual = temp.toCelsius(); double expected = 86.0; assertEquals(expected, actual); }` – Benjamin Eckardt Jun 01 '20 at 09:31
-
This is what I have done. package JunitAssignment; import static org.junit.Assert.*; import org.junit.Test; public class Testing { @Test public void testcelsius() { Temperature temp = new Temperature(30); double result = temp.toCelsius(); double expected = 86; assertEquals(expected, result); } public void testfahrenheit() { Temperature temp1 = new Temperature(86); double result1 = temp1.toCelsius(); double expected1 = 30; assertEquals(expected1, result1); } } – Dave Young Jun 01 '20 at 17:22
-
When I run it, it only says testcelsius. Should it also include test Fahrenheit or should I create a new class for testfahrenheit? – Dave Young Jun 01 '20 at 17:23
-
What I'm asking is I get a result of Runs1/1 Errors:0 and Failures:1 and Testcelsius=0.031 sec Should I get that? Actually, now I see the testfahrenheit, but I'm getting failures both times – Dave Young Jun 01 '20 at 17:24
-
Also for some reason, now for my Temperatureconverter.java , for converter = new Temperature(); I get an error under new Temperature(); saying the constructor is undefined – Dave Young Jun 01 '20 at 17:30
-
Because you created a constructor, the default constructor is gone. See https://stackoverflow.com/questions/4488716/java-default-constructor – Benjamin Eckardt Jun 01 '20 at 18:24
-
-
I fixed it but still get a failurepackage JunitAssignment; import static org.junit.Assert.*; import org.junit.Test; public class Testing { @Test public void testcelsius() { Temperature temp = new Temperature(); temp.setCelsius(30.0); double result = temp.toFahrenheit(); double expected = 86.0; assertEquals(expected, result); } – Dave Young Jun 02 '20 at 00:49