No tests found matching Method addtest - java

I am learning Junit Testing, and I decided to make a simple calculator and test it. I created Calculation_JUnit and Calculation_JUnit_Test
Also i have a problem of using class methodsenter image description here
package com.example;
public class Calculation_JUnit {
public static int add(int a, int b){
return a+b;
}
public static int sub(int a, int b){
return a-b;
}
public static int mul(int a, int b){
return a*b;
}
public static int div(int a, int b){
return a/b;
}
}
Calculation_JUnit_Test
import org.junit.*;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
import com.example.Calculation_JUnit;
public class Calculation_JUnitTest {
#Before
public void setUp() {
System.out.println("Test is passedd ");
}
#Test
public void Add_Test ()
{
Calculation_JUnit calculation_jUnit = new Calculation_JUnit();
Assert.assertEquals("Good",5, calculation_jUnit.add(2,1));
}
#Test
public void Sub_Test ()
{
Calculation_JUnit calculation_jUnit = new Calculation_JUnit();
Assert.assertEquals("Not Good",5, calculation_jUnit.add(5,1));
}
#Test
public void Mult_Test ()
{
Calculation_JUnit calculation_jUnit = new Calculation_JUnit();
Assert.assertEquals("Not Good",3, calculation_jUnit.add(5,1));
}
#Test
public void Div_Test ()
{
Calculation_JUnit calculation_jUnit = new Calculation_JUnit();
Assert.assertEquals("Not Good",5, calculation_jUnit.add(4,1));
}
}

Related

How to mock for custom Event?

MyEvent extends EventObject.
public class MyEvent extends EventObject {
private int buttonName;
public void setNum( int num) {
this.num= num;
}
public int getNum(){
return num;
}
public MyEvent(Object source) {
super(source);
}
}
With mockito-all-2.0.2-beta, I do mock the above Event for a unit test
import org.junit.After;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
public class MyEventTest {
public MyEventTest() {
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
/**
* Test of getButtonNum method, of class MyEvent.
*/
#Test
public void testGetButtonNumEqual() {
System.out.println("setButtonNum");
MyEvent evt = mock(MyEvent.class);
int buttonNum = 1;
evt.setButtonNum(buttonNum);
int result = evt.getButtonNum();
System.out.println(buttonNum);
System.out.println(result);
assertEquals(buttonNum, result);
}
/**
* Test of getButtonNum method, of class MyEvent.
*/
#Test
public void testGetButtonNumNotEqual() {
System.out.println("setButtonNum");
MyEvent evt = mock(MyEvent.class);
int buttonNum = 2;
int notEqualNum = 1;
evt.setButtonNum(buttonNum);
int result = evt.getButtonNum();
System.out.println(buttonNum);
System.out.println(result);
assertNotEquals(notEqualNum, result);
}
}
First Test is failed and Second Test is passed. The print output is below.
{
setButtonNum
1
0
and
setButtonNum
2
0
}
I would like to know why the first test is fail and how to do unit test a custom event.
Please, let me know what mistake I did. I appreciate your help. Thanks.

How can I keep a java program running in the background? [duplicate]

I have put together a JNA code for installing keyboard hook in Windows (using the JNA examples). The code compiles and everything, and I get the hook installed (I get handle to the hook successfully), also I can uninstall the hook successfully. However, the callback never get called when I press any key on the keyboard. Here is my code (most of it are type definitions got from the JNA examples, go to the "main" directly for my part)
import com.sun.jna.IntegerType;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.FromNativeContext;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Library;
import com.sun.jna.win32.W32APITypeMapper;
import com.sun.jna.win32.W32APIFunctionMapper;
import java.util.Map;
import java.util.HashMap;
public class HelloWorld {
static Map UNICODE_OPTIONS = new HashMap() {
{
put("type-mapper", W32APITypeMapper.UNICODE);
put("function-mapper", W32APIFunctionMapper.UNICODE);
}
};
public static class LONG_PTR extends IntegerType {
public LONG_PTR() { this(0); }
public LONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class UINT_PTR extends IntegerType {
public UINT_PTR() { super(Pointer.SIZE); }
public UINT_PTR(long value) { super(Pointer.SIZE, value); }
public Pointer toPointer() { return Pointer.createConstant(longValue()); }
}
public static class ULONG_PTR extends IntegerType {
public ULONG_PTR() { this(0); }
public ULONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class LRESULT extends LONG_PTR {
public LRESULT() { this(0); }
public LRESULT(long value) { super(value); }
}
public static class WPARAM extends UINT_PTR {
public WPARAM() { this(0); }
public WPARAM(long value) { super(value); }
}
public static class LPARAM extends LONG_PTR {
public LPARAM() { this(0); }
public LPARAM(long value) { super(value); }
}
public static class KBDLLHOOKSTRUCT extends Structure {
public int vkCode;
public int scanCode;
public int flags;
public int time;
public ULONG_PTR dwExtraInfo;
}
static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
{ super.setPointer(Pointer.createConstant(-1)); }
public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};
public static class HANDLE extends PointerType {
public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}
public static class HHOOK extends HANDLE { }
public static class HINSTANCE extends HANDLE { }
public static class HMODULE extends HINSTANCE { }
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32)Native.loadLibrary("user32", User32.class, UNICODE_OPTIONS);
static final int WH_KEYBOARD_LL = 13;
public static interface HOOKPROC extends StdCallCallback {
LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam);
}
HHOOK SetWindowsHookEx(int idHook, HOOKPROC lpfn, HMODULE hMod, int dwThreadId);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, Pointer lParam);
boolean UnhookWindowsHookEx(HHOOK idHook);
}
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class, UNICODE_OPTIONS);
HMODULE GetModuleHandle(String name);
}
public static HHOOK hHook;
public static User32.HOOKPROC lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
System.out.println(hMod);
lpfn = new User32.HOOKPROC() {
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
System.out.println(hHook);
if(hHook != null)
System.out.println("Keyboard hooked, type anything to quit");
while(!quit) {
Thread.sleep(100);
}
if(User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
I have done keyboard/mouse hooks several times using both C++ and C# in the past. This my first attempt with Java, and I just don't know whether I imported and mapped the library correctly. Any ideas?
Thank you.
It appears you need to call GetMessage or PeekMessage, which is odd - it is not mentioned in the documentation for Hooks or LowLevelKeyboardProc. I don't know enough about this part of the API to guess at the reason.
I just used the example classes:
import com.sun.jna.examples.win32.*;
public class Callback {
public static User32.HHOOK hHook;
public static User32.LowLevelKeyboardProc lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
W32API.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
lpfn = new User32.LowLevelKeyboardProc() {
public W32API.LRESULT callback(int nCode, W32API.WPARAM wParam,
User32.KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam
.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod,
0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
while (!quit) {
User32.INSTANCE.PeekMessage(msg, null, 0, 0, 0);
Thread.sleep(100);
}
if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
A minimalist example:
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HINSTANCE;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser.HOOKPROC;
public class MainTestKeyHook {
public static void main(String[] args) throws Exception {
HOOKPROC hookProc = new HOOKPROC_bg();
HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
User32.HHOOK hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, hookProc, hInst, 0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
System.err.println("Please press any key ....");
while (true) {
User32.INSTANCE.GetMessage(msg, null, 0, 0);
}
}
}
class HOOKPROC_bg implements HOOKPROC {
public HOOKPROC_bg() {
}
public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
System.err.println("callback bbbnhkilhjkibh nCode: " + nCode);
return new LRESULT(0);
}
}

How to find how many methods and their names used my variable in a java program?

Here is example to find how many variables used in my class. But I need to find how many methods are using my variable with in a class.
Test class having four methods but sample1 variable is used in test1() and test3() methods. I want output as test1(),test2() are used sample1 variable
import java.lang.reflect.Field;
public class Test {
private int sample1;
private int sample2;
private int sample3;
public void test1()
{
System.out.println(sample1);
}
public void test2()
{
System.out.println(sample2);
}
public void test3()
{
System.out.println(sample1);
}
public void test4()
{
System.out.println(sample3);
}
public static void main(String[] args) {
Test t = new Test();
Field f[] =Test.class.getDeclaredFields();
for (int i = 0; i < f.length; i++)
{
System.out.println("Variable Name is : " + f[i].getName());
}
}
}
Can this help?
import java.lang.reflect.Field;
import java.util.*;
public class Main {
private int sample1;
private int sample2;
private int sample3;
private ArrayList<String> whoUseSameple1= new ArrayList<String>();
int getSample1(String methodname){
whoUseSameple1.add(methodname);
return sample1;
}
public void test1(){
System.out.println(getSample1(new Object(){}.getClass().getEnclosingMethod().getName()));
}
public void test2(){
System.out.println(sample2);
}
public void test3(){
System.out.println(getSample1(new Object(){}.getClass().getEnclosingMethod().getName()));
}
public void test4(){
System.out.println(sample3);
}
public static void main(String[] args) {
Main t = new Main();
t.test1();
t.test2();
t.test3();
t.test4();
for (String s:t.whoUseSameple1){
System.out.print(s+" ");
}
System.out.print("are usd Sample1 Variable\n");
// Field f[] =Main.class.getDeclaredFields();
// for (int i = 0; i < f.length; i++)
// {
// System.out.println("Variable Name is : " + f[i].getName());
// }
}
}

Java / JUnit - comparing two polynomial objects

I have a Java class called Term holding polynomials like below
public Term(int c, int e) throws NegativeExponent {
if (e < 0) throw new NegativeExponent();
coef = c;
expo = (coef == 0) ? 1 : e;
}
I also have an equals method in the same class like below
#Override
public boolean equals(Object obj) {
}
I am stuck with how to code how to compare these 2 Term objects
Within my JUnit test file I am using the test below to try and test the equals method
import static org.junit.Assert.*;
import org.junit.Test;
public class ConEqTest
{
private int min = Integer.MIN_VALUE;
private int max = Integer.MAX_VALUE;
#Test
public void eq01() throws TError { assertTrue(new Term(-10,0).equals(new Term(-10,0))); }
#Test
public void eq02() throws TError { assertTrue(new Term(0,0).equals(new Term(0,2))); }
What's wrong with
#Override
public boolean equals(Object obj) {
if (! (obj instanceof Term))
return false;
Term t = (Term)obj;
return coef == t.coef && expo == t.expo;
}
import static org.junit.Assert.*;
import org.junit.*;
#SuppressWarnings("serial") class NegativeExponentException extends Exception {}
class Term {
#Override public int hashCode() {
final int prime=31;
int result=1;
result=prime*result+coefficient;
result=prime*result+exponent;
return result;
}
#Override public boolean equals(Object obj) {
if(this==obj)
return true;
if(obj==null)
return false;
if(getClass()!=obj.getClass())
return false;
Term other=(Term)obj;
if(coefficient!=other.coefficient)
return false;
if(exponent!=other.exponent)
return false;
return true;
}
public Term(int c,int e) throws NegativeExponentException {
if(e<0)
throw new NegativeExponentException();
coefficient=c;
exponent=(coefficient==0)?1:e;
}
int coefficient,exponent;
}
public class So13408797TestCase {
#Test public void eq01() throws Exception {
assertTrue(new Term(-10,0).equals(new Term(-10,0)));
}
#Test public void eq02() throws Exception {
assertTrue(new Term(0,0).equals(new Term(0,2)));
}
private int min=Integer.MIN_VALUE;
private int max=Integer.MAX_VALUE;
}

JNA Keyboard Hook in Windows

I have put together a JNA code for installing keyboard hook in Windows (using the JNA examples). The code compiles and everything, and I get the hook installed (I get handle to the hook successfully), also I can uninstall the hook successfully. However, the callback never get called when I press any key on the keyboard. Here is my code (most of it are type definitions got from the JNA examples, go to the "main" directly for my part)
import com.sun.jna.IntegerType;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.FromNativeContext;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Library;
import com.sun.jna.win32.W32APITypeMapper;
import com.sun.jna.win32.W32APIFunctionMapper;
import java.util.Map;
import java.util.HashMap;
public class HelloWorld {
static Map UNICODE_OPTIONS = new HashMap() {
{
put("type-mapper", W32APITypeMapper.UNICODE);
put("function-mapper", W32APIFunctionMapper.UNICODE);
}
};
public static class LONG_PTR extends IntegerType {
public LONG_PTR() { this(0); }
public LONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class UINT_PTR extends IntegerType {
public UINT_PTR() { super(Pointer.SIZE); }
public UINT_PTR(long value) { super(Pointer.SIZE, value); }
public Pointer toPointer() { return Pointer.createConstant(longValue()); }
}
public static class ULONG_PTR extends IntegerType {
public ULONG_PTR() { this(0); }
public ULONG_PTR(long value) { super(Pointer.SIZE, value); }
}
public static class LRESULT extends LONG_PTR {
public LRESULT() { this(0); }
public LRESULT(long value) { super(value); }
}
public static class WPARAM extends UINT_PTR {
public WPARAM() { this(0); }
public WPARAM(long value) { super(value); }
}
public static class LPARAM extends LONG_PTR {
public LPARAM() { this(0); }
public LPARAM(long value) { super(value); }
}
public static class KBDLLHOOKSTRUCT extends Structure {
public int vkCode;
public int scanCode;
public int flags;
public int time;
public ULONG_PTR dwExtraInfo;
}
static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
{ super.setPointer(Pointer.createConstant(-1)); }
public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};
public static class HANDLE extends PointerType {
public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}
public static class HHOOK extends HANDLE { }
public static class HINSTANCE extends HANDLE { }
public static class HMODULE extends HINSTANCE { }
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32)Native.loadLibrary("user32", User32.class, UNICODE_OPTIONS);
static final int WH_KEYBOARD_LL = 13;
public static interface HOOKPROC extends StdCallCallback {
LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam);
}
HHOOK SetWindowsHookEx(int idHook, HOOKPROC lpfn, HMODULE hMod, int dwThreadId);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, Pointer lParam);
boolean UnhookWindowsHookEx(HHOOK idHook);
}
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class, UNICODE_OPTIONS);
HMODULE GetModuleHandle(String name);
}
public static HHOOK hHook;
public static User32.HOOKPROC lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
System.out.println(hMod);
lpfn = new User32.HOOKPROC() {
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
System.out.println(hHook);
if(hHook != null)
System.out.println("Keyboard hooked, type anything to quit");
while(!quit) {
Thread.sleep(100);
}
if(User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
I have done keyboard/mouse hooks several times using both C++ and C# in the past. This my first attempt with Java, and I just don't know whether I imported and mapped the library correctly. Any ideas?
Thank you.
It appears you need to call GetMessage or PeekMessage, which is odd - it is not mentioned in the documentation for Hooks or LowLevelKeyboardProc. I don't know enough about this part of the API to guess at the reason.
I just used the example classes:
import com.sun.jna.examples.win32.*;
public class Callback {
public static User32.HHOOK hHook;
public static User32.LowLevelKeyboardProc lpfn;
public static volatile boolean quit = false;
public static void main(String[] args) throws Exception {
W32API.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
lpfn = new User32.LowLevelKeyboardProc() {
public W32API.LRESULT callback(int nCode, W32API.WPARAM wParam,
User32.KBDLLHOOKSTRUCT lParam) {
System.out.println("here");
quit = true;
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam
.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod,
0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
while (!quit) {
User32.INSTANCE.PeekMessage(msg, null, 0, 0, 0);
Thread.sleep(100);
}
if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
A minimalist example:
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HINSTANCE;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser.HOOKPROC;
public class MainTestKeyHook {
public static void main(String[] args) throws Exception {
HOOKPROC hookProc = new HOOKPROC_bg();
HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
User32.HHOOK hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, hookProc, hInst, 0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
System.err.println("Please press any key ....");
while (true) {
User32.INSTANCE.GetMessage(msg, null, 0, 0);
}
}
}
class HOOKPROC_bg implements HOOKPROC {
public HOOKPROC_bg() {
}
public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
System.err.println("callback bbbnhkilhjkibh nCode: " + nCode);
return new LRESULT(0);
}
}

Categories