Test case to check if attribute is set to private (Java)

Test case to check if attribute is set to private (Java)

de Tommi Saksa -
Número de respuestas: 3

Is there any way to create a test case, where you could confirm, that the student has declared an attribute as private? For example, if I would have the following assignment:

  1. Create a class called "Dog". 
  2. The class has one private attribute "name".
  3. Create getter and setter methods for the name attribute. The name of the getter method should be getName. The name of the setter method should be setName.

Test case could be like:

// Create an object                                
Dog dog = new Dog();
// Assign/set name
dog.setName("Lassie");
// Print name using getter
System.out.println(dog.getName());


This way I can check that the getter and setter methods work, but how can I check that the name attribute is private and not public?


En respuesta a Tommi Saksa

Re: Test case to check if attribute is set to private (Java)

de Matthew Toohey -

Hi Tommi

One way I've just found, is to have a test with the following code.

// Create an object                                
Dog dog = new Dog();

// Check that the name attribute is private
try {
    System.out.println(dog.getClass().getDeclaredField("name"));
} catch (Exception e) {
    System.out.println("You Dog class should have a name attribute!");
}

The expected output for this code is

private java.lang.String Dog.name

This will also test the type and name of the attribute. If you don't wish to be so strict around what the attribute is called (it is private after all). Then you could try getting 'getDeclaredFields' method to get all fields of the class and just print the private/public part.

You may want to checkout the following documentation around this.

https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredField-java.lang.String-

https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredFields--

https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html

https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getModifiers--


I have also attached an export of a working example question using this.



Matthew

En respuesta a Matthew Toohey

Re: Test case to check if attribute is set to private (Java)

de Tommi Saksa -
Thanks Matthew!

This works perfectly and is easy to implement.

I was just thinking that if the attribute is public (should be private), the output could be more informative. It's now like: 

public int dog.age

It could be like: "Check the access modifier" or something...
En respuesta a Tommi Saksa

Re: Test case to check if attribute is set to private (Java)

de Matthew Toohey -

I agree that output like that would be much nicer. One way this could be achieved would be to customise the template and add a function (that would be hidden from students) that checks for this. The test would then become something like checkAccessModifiers() and this function would do all the checks and print out informative information to students. If they have done everything right the function might just print something like "Access modifiers correct" and if they have done something wrong the function could print out an explanation of what is incorrect.

There are many ways to do more advanced checking with coderunner. However, the default java question types are quite limiting. Which is why you may want to consider customising question templates or defining your own question prototypes.


Matthew