-1

I'm new to Java and needed some help. I have a utility class that has two methods where one method converts Json string to object and the other converts an object to Json string

I wanted to test these methods with junit. For this test purpose I would need an object ofcourse. Lets say a student object with a name and StudentId.

My question is that is there any way I can get this object in my Test without actually creating a separate class for the student? Since I only need this object for testing purposes.

Qad
  • 51
  • 3
  • 7
  • 1
    Why would you need a separate class? – shmosel Aug 21 '16 at 17:56
  • 1) "string to object" test: Assemble a valid JSON string, pass it to your method, then _assert that_ the returned object (a student) has the properties you expect. 2) "object to string" test: Create an instance of your student class, pass that to your method, then assert that the returned string is a valid JSON string representing your student. – Seelenvirtuose Aug 21 '16 at 18:05
  • @shmosel I just need any object for the test. Based on what I know, wouldn't the object need to have a class? In this case a Student class? – Qad Aug 21 '16 at 18:06
  • @Seelenvirtuose I don't have any student class. I just need a test object. So should i create a student class for the test? – Qad Aug 21 '16 at 18:09
  • So you're testing that serialization works for any type of object? You can't create an object without a class. You can use an anonymous subclass to reduce clutter, but that won't work for deserialization. I would say just create a static nested class, or even a local class if it's restricted to a single method. – shmosel Aug 21 '16 at 18:12
  • 1
    Well ... tell us the methods' signatures. – Seelenvirtuose Aug 21 '16 at 18:50
  • I agree, your requirements are simply ... strange. The answers you got so far make all sense; to a certain degree; but with your unclear input, they are just blind shots. Maybe right; maybe not so much. – GhostCat Aug 22 '16 at 04:04

3 Answers3

1

Why create another Student class? Just create a Student object in your junit test class (tests have to be separate from code of course) It will be something like this:

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

public class StudentTest
{

    private Student tester;

    @Before
    public void setUp() throws Exception
    {
        //init student objects
    }

    @Test
    public void test()
    {
        //test logic
    }

    @After
    public void cleanUp()
    {
        //destruct
    }
}

see the junit documentation for an explanation of test logic, e.g. AssertNotNull If your class isn't called student, take the class you ment on your question (your utility class?)

Ueda Ichitaka
  • 71
  • 1
  • 8
  • oh and, unlike the other comments, you don't explictly need to create a class. You can take your utility class for the test and simulate the outputs by feeding the methods with a stream. It just isn't very clean because of potential code duplicates. You basically create new objects, give them teir input, eg the JSON, test and destruct – Ueda Ichitaka Aug 21 '16 at 18:18
0

If you don't want to create a separate class, you could just create a static member class in your JUnit test class as shown in the example below:

public class StaticMemberClassDemo {

    public static void main(String[] args) {
        Student student = new Student("John Doe", 1);
    }

    private static class Student {
        private final String name;
        private final int id;

        public Student(String name, int id) {
            this.name = name;
            this.id = id;
        }
    }
}
rohitvats
  • 1,811
  • 13
  • 11
0

I am not sure what you mean by a separate class here. Just note that in java you can define different types of nested classes. Here it would not hurt to define a static nested class as rohitvats suggested.

milpac
  • 31
  • 4