I am not even sure if this is possible, but I came up with this idea because of this question: Change private static final field using Java reflection
So this is the scenario:
public static String test() {
return "test test test";
}
What I want to do this let the test() method return "worked" instead of "test test test".
Does anybody have an idea how to accomplish this?
EDIT:
I downloaded powermock and tried this:
package test;
import org.powermock.api.easymock.PowerMock;
public class InjectorTest {
public static void main(final String[] args) {
System.out.println("Before the injection:");
System.out.println(test());
PowerMock.mockStatic(InjectorTest.class);
PowerMock.doReturn("worked").when(InjectorTest.class, "test");
System.out.println("After the injection:");
System.out.println(test());
}
public static String test() {
return "did not work :(";
}
}
But eclipse gives this error: The method doReturn(String) is undefined for the type PowerMock
Did I download the wrong one or was that a bad sample code?
Because I don't want more votedowns, WHY I want to do this?
I want to inject Minecraft in that way that it doesn't uses the user.home but a relative URI.
In this way I can make a pre-installed portable Minecraft for an USB stick for school :D
I think PowerMock can do this. (Java reflection under the hood)
With PowerMock, your code would look like this:
public static void main(String[] args) {
PowerMock.mockStatic(Foo.class);
PowerMock.doReturn("worked").when(Foo.class, "test");
System.out.println(Foo.test());
}
Credits to #RC for code above
Related
Programming Language : Java
Testing tool : powermockito,Junit
I have a static block in my code. I am using that code particle as several reference. While during integration testing (using powermockito) I mocked a getWebDetails() method in the below example. The method for mocking (getWebDetails) is also getting called from the static block in my program, so it excecutes before my test gets excecuted and returns a null value. I need to assign values to xml contents during my testing.
class test1{
private static String xmlcontents;
static {
xmlcontents= ServiceUtils.getWebDetails();
}
public static String getWebDetails() throws Exception{
url = test2.getURL();
return url;
}
}
Nota bene (please note): I tried using #SuppressStaticInitializationFor("test1"), but it suppresses all the static methods inside the class.
Can anyone help me? Any help will be highly appreciable..
I am trying to understand Unit testing and how the correct classes are being fetched on test time,
I'm having a hard time understanding exactly what is going on behind the scenes and how safe/correct my usages for this are.
This is a very simple example of what i am trying to do, i wrote it here inline so it probably contains some errors, please try to ignore them, as stupid as they may be.
A very simple project directory:
ba/src/main/java/utils/BaUtils.java
ba/test/main/java/utils/BaUtilsTest.java
notBa/src/main/java/im/BaObj.java
BaUtils.java code:
package com.ba.utils;
import notBa.im.BaObj;
public class BaUtils{
public String doSomething(BaObj obj){
obj.doSomething();
}
}
I would like to test BaUtils wihtout actually calling doSomething, and i can't change anything on BaObj class or notBa package. I know that i can (by 'can' i mean it will work) add a new java file to 'ba' project (ba/test/java/notBa/main/java/im/BaObj.java) that will have the same package as the original BaObj, and at runtime the test will import this one instead of the real one, so BaUtils code is tested but BaObj code is not excecuted.
that should look something like like :
package notBa.im.Baobj
public class BaObj{
public void doSomething(){
System.out.println("Did something");
}
}
My questions are (And thank you for reaching this far):
How does this work (Reading references would be great).
Is this kind of test building considered 'good' or 'safe' ?
Thanks!
The solution is to use a mocking framework (I for myself like Mockito).
The test would look like this:
class BlaUtilTes{
#Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
#Mock
Blaobj blaobj;
#Test
public void doSomething_WithMockedBlaobj_callsDosomethingOnBlaobj(){
// arrange
BlaUtil blaUtil= new BlaUtil();
// act
blaUtil.doSomething(blaobj);
// assert
Mockito.verify(blaobj).doSomething();
}
}
find more information here http://www.vogella.com/tutorials/Mockito/article.html#testing-with-mock-objects
Your BaUtilsTest class should look like this.. I have used mockito for for mocking external dependencies. Also I changed the method return type to String for easy understanding.
#RunWith(MockitoJUnitRunner.class)
class BaUtilsTest {
BaUtils util;
#Mock
BaObj mockBaObj;
#Before
public void setup() {
util = new BaUtils();
}
#Test
public void testDoSomething() {
Mockito.when(mockBaObj.doSomething()).thenReturn("did the work using mock");
String result = util.doSomething(mockBaObj);
Assert.assertEquals("did the work using mock", result);
}
}
I am trying to write a test suite using JUnit4 by relying on JUnit4TestAdapter. Having a look at the code of this class I saw that it only works with a Class as input. I would like to build a test class and set a parameter on it before running it with my TestSuite. Unfortunately, Junit4TestAdapter is building the test by using reflection (not 100% sure about the mechanism behind it), which means that I cannot change my test class on runtime.
Has anybody done anything similar before? Is there any possible workaround to this issue? Thanks for your help!
public class SimpleTest {
#Test
public void testBasic() {
TemplateTester tester = new TemplateTester();
ActionIconsTest test = new ActionIconsTest();
test.setParameter("New Param Value");
tester.addTests(test);
tester.run();
}
}
/////
public class TemplateTester {
private TestSuite suite;
public TemplateTester() {
suite = new TestSuite();
}
public void addTests(TemplateTest... tests) {
for (TemplateTest test : tests) {
suite.addTest(new JUnit4TestAdapter(test.getClass()));
}
}
public void run() {
suite.run(new TestResult());
}
}
/////
public interface TemplateTest {
}
/////
public class ActionIconsTest extends BaseTestStrategy implements TemplateTest {
#Test
public void icons() {
//Test logic here
}
public void navigateToTestPage() {
//Here I need the parameter
}
}
/////
public abstract class BaseTestStrategy {
protected String parameter;
#Before
public void init() {
navigateToTestPage();
}
public abstract void navigateToTestPage();
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
I am trying to test a web application with Selenium. The way I want to test is by splitting the functionality, e.g., I want to test the available icons (ActionIconsTest), then I'd like to test other parts like buttons, etc.
The idea behind this is to have a better categorization of the functionality available in certain screen. This is quite coupled with the way we are currently developing our web app.
With this in mind, TemplateTest is just an interface implemented by the different kind of tests (ActionIconTest, ButtonTest, etc) available in my system.
TemplateTester is a Junit suite test with all the different tests that implement the interface TemplateTest.
The reason for this question is because I was trying to implement a Strategy pattern and then realized of the inconvenient of passing a class to Junit4TestAdapter in runtime.
Well, taking in account that JUNIT needs your tester's Class object as an object factory (so he can create several instances of your tester), I can only suggest you pass parameters to your tester through System Properties.
Moreover, it's the recommended way of passing parameters: http://junit.org/faq.html#running_7
I have written simple java program:
package bsh;
import test.Testclass;
public class Whatever {
public static void main(String args[]){
Testclass t = new Testclass();
System.out.println(t.squareIt(8));
}
}
package test;
public class Testclass {
public Testclass(){
}
public int squareIt(int i){
return i*i;
}
}
I have two questions about this java program:
How to execute this java program from jmeter?
How to call sqaureIt(int i) method from jmeter?
How can i achieve this?
I haven't tried main class execution , but i have certainly executed Junit Testcases through Jmeter
Have a look at this doc Junitsampler tutorial
Aside from following the tutorial as Sudhakar mentioned...
Your main test case must extend TestCase or some form of it. Your test method must begin with the word test or use annotations.
Do not use a void main method as in a normal java application.
It will automatically call and run your method that starts with the name test.
So you could do this:
public class Whatever extends TestCase {
public void testIt() {
//test code here
new Testclass().squareIt(5);
}
}
JMeter is typically used for testing performance on web applications. Correct me if I'm wrong, but unless you plan on converting this into some sort of web app, you should try using VisualVM to measure your program's performance.
I'm using Fedora and I have had some issues to get javac to work (I finally succeeded by making an alias). But now I can't execute my java code. I get the error in the title.
Here is the class that contains the main method:
public class test
{
public static void main(String args)
{
int res[]= {4,2,6};
res=Trieur.tri(res);
for(int i: res)
System.out.println(i);
}
}
I've been trying a lot of solutions in this forum but none seems to work. The program compiles successfully.
Can you please help me?
change this:
public static void main(String args[])
Or as public static void main(String[] args). Either syntax is valid in Java, although this format is arguably slightly more popular.