I have a module which implements configure() method of Guice AbstractModule class. I bind() object instances in this method simply as follows:
bind(Command.class)
.annotatedWith(Names.named("commandSpringBean"))
.toProvider(
SpringIntegration.fromSpring(Command.class, "commandSpringBean"));
How can I write a JUnit to test this method?
Related
I need to mock the following Model Mapper Strict strategy configuration with the mockito.
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
I tried the following in my test method but i am getting Null Pointer Exception.
#Test
public void mockModelMapper(){
when(modelmapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT))
.thenReturn(modelmapper.getConfiguration());
}
Thanks in advance.
As mentioned by #DCTID, you can't chain the mocks. You need to split each method call and inject a mock for the object returned by getConfiguration().
#Test
public void mockModelMapper(){
// Inject the configuration mock
Configuration configurationMock = mock(Configuration.class);
when(configurationMock.setMatchingStrategy(MatchingStragegies.STRICT)
.thenReturn(configuraitonMock);
when(modelmapper.getConfiguration()).thenReturn(configurationMock);
}
Cucumber supports hooks -- methods that run before or after a scenario.
The #Before and #After annotations are used to mark them.
A method with #Before annotation will run before each scenario, #After -- after each scenario.
An example of a class with hooks:
public class Hooks {
#Before
public void init() {
System.out.println("before each Cucumber scenario");
}
#After
public void stop() {
System.out.println("after each Cucumber scenario");
}
}
Can you tell me, please, what annotations I must use in order to run method 1 time before the entire group of Cucumber-scenarios (feature-files)?
If there is no such annotation, then how can we do it in another way?
You can use the standard Junit annotation #BeforeAll and #AfterAll
#BeforeAll methods are only executed once for a given test class.
#BeforeAll is used to signal that the annotated method should be executed before all tests in the current test class.
Please refer this documentation #BeforeAll
We have many test classes that are annotated with #Before and #After to perform test construction/destruction.
But because we now need to perform actions on test failure but prior to #After executing we have used a RuleChain to handle test construction / destruction ourselves. It just points back to the setUp() and tearDown() methods (yes we were lucky that most developers had stuck to that convention).
The problem is that JUnit still invokes the methods annotated with #Before and #After as well as invoking them via the new Rule.
At it's core JUnit is invoking
org.junit.internal.runners.statements.RunAfters.evaluate
for the innermost statement.
One option is obviously removing the #Before and #After annotations from the Tests, but we are talking about many hundreds of tests.
How can we switch off processing of #Before and #After ?
I don't think you can flick a JUnit 'switch' to disable this behaviour. JUnit offers two ways of changing test execution behaviour:
Extend BlockJUnit4ClassRunner
Add a Rule to your test case
Both of these require you to change your test cases, of these the second option might be least invasive (though that does depend on whether your test cases are already using customised runners).
Given this simple JUnit runner:
public class IgnoreBeforeAndAfter extends BlockJUnit4ClassRunner {
public IgnoreBeforeAndAfter(Class<?> klass) throws InitializationError {
super(klass);
}
protected Statement withBefores(FrameworkMethod method, Object target,
Statement statement) {
return statement;
}
protected Statement withAfters(FrameworkMethod method, Object target,
Statement statement) {
return statement;
}
}
The following test case will pass (thereby proving that the #Before and #After methods are not invoked):
#RunWith(IgnoreBeforeAndAfter.class)
public class SimpleTest {
#Before
public void setUp() {
Assert.fail("The #Before method should be ignored!");
}
#After
public void tearDown() {
Assert.fail("The #After method should be ignored!");
}
#Test
public void canIgnoreBeforeAndAfter() {
assertTrue(true);
}
}
A structural search+replace on your code base could be used to add this annotation to all test cases which contain at least one of #Before or #After.
Though of course a structural search+replace could also remove all #Before and #After annotations from your code.
An approach which requires no changes to your existing test cases (but which is somewhat non standard) would be to provide no-op implementations of ...
org.junit.internal.runners.statements.RunAfters
org.junit.internal.runners.statements.RunBefores
... in your own code base, these would 'replace' the JUnit equivalents and if their evaluate() method body was empty then your use of the #Before and #After annotations would have no effect.
I want to test SomeClass methods.
For that, I need SomeClass instance in every test so I'm using #Before annotation and initiate an instance of SomeClass named SC.
The problem is:- How can I test the constructor function after I already use it? It doesn't make sense.
Additional question:- The constructor can get number of arguments and they can influnce the methods outputs, should I mock this class instead of creating an instance of it?
public class SomeClassTest {
SomeClass SC;
#Before
public void initlize() throws IOException{
SC= new SomeClass (argument1,argument2,..);
}
#Test
public void ConstructorTest() {
}
Just don't use the object SC in your ConstructorTest. If you wan't to test a certain outcome from the construction of a SomeClass object with certain parameters then just construct it as such within your ConstructorTest and then assert the relevant outcomes you expect on the newly constructed object.
And no you shouldn't be mocking this class. The test is for testing this class so if you mock it's behaviour then you aren't really testing anything.
Hi I have a class which the invoke the run() method of a thread from the constructor of the class by calling the start() method , So please help me to Stubs the so to write the junit test cases . The class is as follows
public class MyClass extends Thread {
Student st=null;
University uni= new University();
public MyClass(Student st) {
this.st=st;
start();
}
public void run() {
uni.calculate(st);
}
}
Thanks
Take a look at the discussion here:
Testing Constructor With Powermock
It discusses sub-classing and overriding.
In general it should be considered bad practice to have to mock the class under test in order to test it. It is also hard to do since most mocking frameworks will not allow mocking a single method once in the class under test since they create wrapping proxies.