-3

Below is my source code class where class constructor starting the thread.But in run() it is checking for not null value of a varible.So in order to test the using junit that variable should not be null.

public class MainClass extends Thread {
    private SomeQue que;
    private static final String THREAD_NAME = "s_thread";
    private boolean isRunning = false;

    public MainClass () {
        setName(THREAD_NAME);
        setIsRunning(true);
        start();
    }

    public void run() {
        while (isRunning()) {
            if (que!= null) {
                obj = que.pop();
                if (obj != null) {
                    //....
                }
            }
        }
    }
}

If use

ReflectioinTerstUtils.setField(new MainClass(),"que",que);      

we have to create the object to set data to variable to while creating object itself thread is getting started.So any ideas..

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
mae
  • 117
  • 2
  • 12
  • 5
    This is a bad design to be sure. Not only should you not be extending thread, but you shouldn't be starting the thread from within its own constructor. – Hovercraft Full Of Eels May 09 '19 at 10:41
  • 1
    So (1) set it in the constructor, or (2) don't start the thread in the constructor. This seems rather obvious. You also need to look into declaring `que` as `volatile`, and please note that the correct spelling is `queue`. – user207421 May 09 '19 at 10:41
  • 2
    Code formatted for readability. In the future, please put in this effort yourself. – Hovercraft Full Of Eels May 09 '19 at 10:42
  • 1
    SomeQue should be a constrcutor arg in MainThread, aka MainThread(SomeQue queue){ this.que=queue ;.... – akshaya pandey May 09 '19 at 10:44
  • @ akshaya pandey , that is the source code and i'm doing code coverage activity so i can not chage the source code.Data coming to this from some where while application is running. – mae May 09 '19 at 10:47
  • @Hovercraft Full Of Eels. Do you mean code is not readable? – mae May 09 '19 at 10:48
  • 1. If you can't change the source code this is not a programming question: 'any ideas' about *what* exactly? 2. Yes he did mean it is illegible. Look at those three `}` with the same levels of indentation. – user207421 May 09 '19 at 10:50
  • I'm voting to close this question as off-topic because the OP states the source code can't be changed, which makes the question both unclear and not about computer programming. – user207421 May 09 '19 at 10:50
  • @user207421 1. I mean I have to do code change in my test case not in source code.2.'any ideas'-- I have no idea for this scenario ,I need clue to proceed. – mae May 09 '19 at 11:02
  • 1
    You can use sun.misc.Unsafe to create objects without calling their constructors. The problem with doing that is it’s an internal (deprecated?) API and the class fields will be initialized to their defaults (null,0,false,etc.) – Benjamin Urquhart May 09 '19 at 11:43
  • @mae: it wasn't *easily* readable as your indentation, especially the curly brace indentation, was somewhat random. You don't want your code harder to read and understand than it has to be. – Hovercraft Full Of Eels May 10 '19 at 02:51

1 Answers1

1

The only way I see around this (besides refactoring) is to create an instance of the object without calling the constructor. This can be done using the internal class sun.misc.Unsafe:

Note: the very name of this class tells you how powerful (and deadly) it can be. With great power comes great responsibility. Use wisely.

// Obtain the unsafe object without throwing a SecurityException (assuming no security manager)
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
Unsafe unsafe = (Unsafe) field.get(null);

// Create the instance 
MainClass instance = (MainClass) unsafe.allocateInstance(MainClass.class); // Constructor is not called

// set isRunning if needed
// do junit stuff
Benjamin Urquhart
  • 1,626
  • 12
  • 18