How to specify which classes to be added in #SuiteClasses in junit4 - junit

In Junit 4 we add test suites using annotations as mentioned below:
#RunWith(Suite.class)
#SuiteClasses({
A.class, B.class })
public class MyTestsuite
{
}
My query is, do we have any way to specify condition to decide which classes i wanted to include in the #SuiteClasses. For example; lets say in the above code, if i wanted to class A if some particular condition is true, else i don't want to add it to #SuiteClasses.
In junit 3.8 we do it using suite() method like mentioned below:
public class MyTestsuite extends TestCase
{
public static Test suite()
{
TestSuite suite = new TestSuite();
if(some_condition_true)
{
suite.addTest(A.suite);
}
suite.addTest(B.suite);
return suite;
}
}
Is there any way we can achieve this using junit 4?

Extend Suite:
#RunWith(MySuite.class)//Customized
#SuiteClasses({
A.class, B.class })
public class MyTestsuite
{}
Define MySuite:
public class MySuite extends Suite {
public MySuite(Class<?> klass, RunnerBuilder builder)
throws InitializationError {
super(klass, builder);
try {
filter(new Filter() {
#Override public boolean shouldRun(Description description) {
return some_condition_true? true : false;
}
#Override
public String describe() {
return "...";
}
});
} catch (NoTestsRemainException e) {
System.out.println("Hey, all test cases are not satisfied your condition.");
}
}
}
plus: Although it can work, I recommend keep the test case simple. It is hard to maintain complex thing.

Shengyuan answer will work.
Another option is to always add the test class, but have an assumeTrue(condition) in your class that will decide when it should run and when it doesn't make sense.
(I like that approach better when the condition is related to test prerequisites: the test itself is the best place to know if it should run or not).

Related

Using TestSuites in JUnit5

I like testing similar types of classes with the same code, so I don't forget to do stuff. Using JUnit4 (technically Junit3) I created classes like this:
#RunWith(AllTests.class)
public class MyPojoTest {
public static TestSuite suite() {
return PojoTestSuite.forPojoClass(MyPojo.class)
// test Serializable is implemented correctly
.serializable()
// test Comparable is implemented correctly
.comparable()
// etc.
.cloneable()
// creates a junit.framework.TestSuite
.create();
}
}
public class PojoTestSuite {
public static PojoTestSuite forPojoClass(Class<?> pojoClass) {
return new PojoTestSuite(pojoClass);
}
public TestSuite create() {
final TestSuite suite = new TestSuite();
suite.addTest(new EqualsTest());
suite.addTest(new HashCodeTest());
if (this.serializable) {
suite.addTest(new SerializeableTest());
}
if (this.cloneable) {
suite.addTest(new CloneableTest());
}
if (this.comparable) {
suite.addTest(new ComparableTest());
}
return suite;
}
// getters and setters, constructor, ...
}
However I wasn't able to implement something like it in pure JUnit4 and now in JUnit5 I can't find anything like it either.
Is there any way to use test suites to create a set of tests dynamically? I'd like to have the tests as small as possible, because having "testEqualsReturnsTrueForSameObject()" fail is a lot more useful than having "testPojo()" fail.

Apply aspect to all methods/actions in a class

I'm working on an ASP.NET MVC 5, I want to log all exceptions that occurs in the controller's actions.
To accomplish this I'm creating a custom aspect using PostSharp (in a dll), there I've already created the code to write the log files, now I want that the aspect can be controller-wide (do not want to apply it by hand to all methods).
The aspect's code looks like this:
using System;
using PostSharp.Aspects;
namespace Banlinea.Ceb.Domain.Aspects
{
public class LogException : OnExceptionAspect
{
public LogException()
{
ApplyToStateMachine = true;
}
public override void OnException(MethodExecutionArgs args)
{
//Code for logging the exception
args.FlowBehavior = FlowBehavior.ThrowException;
}
}
}
Now, what I want in my controller is to do something like this:
[LogException]
public class MyController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Other()
{
return View();
}
public ActionResult Another()
{
return View();
}
}
Just decorate the class, How can I do that?
you can do this byimplementing IAspectProvider
http://doc.postsharp.net/iaspectprovider
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type type = (Type)targetElement;
return type.GetMethods().Select(
m => return new AspectInstance(targetElement, new LogException()) );
}
You can apply PostSharp aspects across your codebase by using a feature called attribute multicasting.
For example, when you apply a method-level aspect on a class level or assembly level, then it is automatically copied to all the methods in the corresponding class or assembly. You can additionally filter the target elements by setting the attribute properties, such as AttributeTargetTypes, AttributeTargetMemberAttributes etc.
The sample code from your question should actually work as you expect.

How to define only last test method assertion will be involved

I have a two class's for testing regression test. We have in some case more than one test method in the class and in the methods we are usually using assertions. I want to know if there any method is available, to make use #Rule test method only the last method in the class. Here is my code:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JustOneClass extends ParentClass {
#Rule
public class GeneralRule articleHotspotRule = new class GeneralRule (this);
#Test
aMethod(){
Assert.assertTrue()
}
#Test
bMethod(){
Assert.assertTrue()
}
#Test
cMethod(){
Assert.assertTrue()
}
#Test
dMethod(){
if this assert is failed Assert.assertTrue()
}
}
We have a another class which extends TestWatcher
public class GeneralRule extends TestWatcher {
private ParentClass baseTest;
public GeneralRule (final GeneralRule generalRule) {
this.baseTest = generalRule;
}
#Override
protected void failed(final Throwable e, final Description description) {
baseTest.after();
}
}
in this case I want that baseTest.after() will be used only if assertion of dMedthod is failed.
Rather than using a rule to try and check for the failure, how about checking for the failure condition and then fail the test programatically? Certainly not as elegant or reusable as a rule but may satisfy your requirement.
#Test
public void dMethod() {
...
if(actual == false) { // check for failure scenario
after(); // call the after method
Assert.fail("hello failure"); // programatically fail the test
}
}

How do I use Mockito to mock a protected method?

I’m using Mockito 1.9.5. How do I mock what is coming back from a protected method? I have this protected method …
protected JSONObject myMethod(final String param1, final String param2)
{
…
}
However, when I attempt to do this in JUnit:
final MyService mymock = Mockito.mock(MyService.class, Mockito.CALLS_REAL_METHODS);
final String pararm1 = “param1”;
Mockito.doReturn(myData).when(mymock).myMethod(param1, param2);
On the last line, I get a compilation error “The method ‘myMethod’ is not visible.” How do I use Mockito to mock protected methods? I’m open to upgrading my version if that’s the answer.
This is not an issue with Mockito, but with plain old java. From where you are calling the method, you don't have visibility. That is why it is a compile-time issue instead of a run-time issue.
A couple options:
declare your test in the same package as the mocked class
change the visibilty of the method if you can
create a local (inner) class that extends the mocked class, then mock this local class. Since the class would be local, you would have visibility to the method.
Responding to the request for a code sample of option 3 from John B's answer:
public class MyClass {
protected String protectedMethod() {
return "Can't touch this";
}
public String publicMethod() {
return protectedMethod();
}
}
#RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
class MyClassMock extends MyClass {
#Override
public String protectedMethod() {
return "You can see me now!";
}
}
#Mock
MyClassMock myClass = mock(MyClassMock.class);
#Test
public void myClassPublicMethodTest() {
when(myClass.publicMethod()).thenCallRealMethod();
when(myClass.protectedMethod()).thenReturn("jk!");
}
}
You can use Spring's ReflectionTestUtils to use your class as it is and without needing of change it just for tests or wrap it in another class.
public class MyService {
protected JSONObject myProtectedMethod(final String param1, final String param2) {
return new JSONObject();
}
public JSONObject myPublicMethod(final String param1) {
return new JSONObject();
}
}
And then in Test
#RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {
#Mock
private MyService myService;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(myService.myPublicMethod(anyString())).thenReturn(mock(JSONObject.class));
when(ReflectionTestUtils.invokeMethod(myService, "myProtectedMethod", anyString(), anyString())).thenReturn(mock(JSONObject.class));
}
}
Something like following worked for me, using doReturn() and Junit5's ReflectionSupport.
[Note: I tested on Mockito 3.12.4]
ReflectionSupport.invokeMethod(
mymock.getClass()
// .getSuperclass() // Uncomment this, if the protected method defined in the parent class.
.getDeclaredMethod("myMethod", String.class, String.class),
doReturn(myData).when(mymock),
param1,
param2);
John B is right, this is because the method you're trying to test is protected, it's not a problem with Mockito.
Another option on top of the ones he has listed would be to use reflection to gain access to the method. This will allow you to avoid changing the method you are testing, and avoid changing the pattern you use to write tests, and where you store these tests. I've had to do this myself for some tests where I was not allowed to change the existing code base which included a large number of private methods that needed to be unit tested.
These links explain Reflection and how to use it very well, so I will link to them rather than copy:
What is reflection and whit is it useful
How to test a class that has private methods, fields, or inner classes
WhiteBox.invokeMethod() can be handy.
public class Test extend TargetClass{
#Override
protected Object method(...) {
return [ValueYouWant];
}
}
In Spring, you can set it high high-priority like this:
#TestConfiguration
public class Config {
#Profile({"..."})
#Bean("...")
#Primary // <------ high-priority
public TargetClass TargetClass(){
return new TargetClass() {
#Override
protected WPayResponse validate(...) {
return null;
}
};
}
}
It is the same to override the origin bean.

How to have individual SetUp functions in Junit

I have two test functions and for each I want to have different #Before methods. How to achieve this ?
Although it seems to be convenient to organize all the test under the same class, for your case I think the best option is to separate the tests into different classes, each one with his corresponding setUp.
An alternative (I prefer the previous option) could be call the setUp directly in your test method, like the example as follows:
public class FooTest {
public void setUpMethod1() {
// do setUp things
}
public void setUpMethod2() {
// do setUp things
}
#Test
public void testMethod1() {
setUpMethod1();
// Test
}
#Test
public void testMethod2() {
setUpMethod2();
// Test
}
}
Only as a curiosity (IMO not recomended for your case), you can override the default junit RunListener with your own implementation. Method testStarted is executed before every test and you have access to class and methodName to be able to identify the running test. Dummy sample:
public class MyRunListener extends RunListener {
#Override
public void testStarted(Description description) throws Exception {
//...
Class testClass = description.getClass();
String methodName = description.getMethodName();
//...
}
}
Hope it helps.