I am trying to write junit tests for a temp converter but have not got one to work yet

StackOverflow https://stackoverflow.com/questions/22141123

  •  19-10-2022
  •  | 
  •  

Question

This is my code and the tests that I have tried so far

        public class TempConvert {

            private double cel;
            private double fah;
            private double kel;


            public TempConvert(double celsius) {
        this.cel = celsius;
        this.fah = (celsius * 1.8) + 32;
        this.kel = celsius + 273.15;
        }

        protected double getCel() {
            return cel;
        }

        public double getfah() {
            return fah;
        }
        public double getkel(){
            return kel;
        }
        public void setCal(double celsius){
            this.cel = celsius;
            this.fah = (celsius * 1.8) + 32;
            this.kel = celsius + 273.15;
        }
        public void setFah(double fahrenheit){
        fah = fahrenheit;
        cel = (fah * 1.8) + 32;
        kel = cel + 273; 

        }
        public void setKel(double kelvin){
            kel = kelvin;
            cel = kel -273;
            fah = (cel * 1.8) + 32;enter code here
        }
        public String toString(){
            //DecimalFormat formater = new DecimalFormat("0.##");
            return "the temp in c is" + cel + "the tempt in f is " + fah +            `"the temp in kel is " + kel;`enter code here`
        }

this is my test page with the ones that I have tried to answer

    import static org.junit.Assert.*;

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    @Test
    public void testSetCal() {
        TempConvert cal1 = new TempConvert(10);
        assertEquals(10,40,283.15,cal1.getCel());
    }

    private void assertEquals(int i, int j, double d, double cel) {
        // TODO Auto-generated method stub

    }

with this test I have tried to use the assert True method but am not sure if this is the right one to use or if this is formatted correctly?

    @Test
    public void testSetFah() {
        TempConvert fah1 = new TempConvert(40);
        assertTrue("range acceptable",fah1.getCel() , fah1.getkel());
    }

    private void assertTrue(String string, double cel, double getkel) {
        // TODO Auto-generated method stub

    }

with this test I have tried the assertEquals method but again not working . I have tried several variations of these two methods and have not gotten any succcess. I am now in fear that my original code  is changed and will have to go back and review it as have tried several auto fixes

    @Test
    public void testSetKel() {
        TempConvert kel1 = new TempConvert(283.15);
        assertEquals(283.15,10,40,kel1.setKel1());//the 3 numbers are the respective values in kelvin and faharenight and celesius
    }

    @Test
    public void testToString() {`enter code here`
        fail("Not yet implemented"); // TODO
    }

any help on any part would be welcomed

Pas de solution correcte

Autres conseils

Why not just make a compareTo method?

public class TempConvert implements Comparable{
        private double cel;
        private double fah;
        private double kel;

        public TempConvert(double c){
          //Find cel, fah, and kel.
        }

        public int compareTo(Object o){
         TempConvert other = (TempConvert)o;
         return this.cel - other.cel;
        }
}

Your test classes are missing the static import for the JUnit Assert class.

import static junit.framework.Assert.*;

I guess that your IDE generated empty methods (assertTrue and assertEquals) because of the missing import. You should remove those methods, as they are not doing anything.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top