JUnit Category - how to avoid code duplication - java

Let's say I have two test classes annotated with proper categories:
#Category({CategoryA.class})
public class A {
...
}
and
#Category({CategoryB.class})
public class B {
...
}
which share common codebase and differ only in 1 line of test method.
What would be the best approach to make this code better?
For example, if I use only one class:
#Category({CategoryA.class, CategoryB.class})
public class AB {
...
}
Is there any way to determine category in runtime and add simple if statement?

Any reason you can't have an abstract class which does most of the work, then two extending classes and have different #Category annotations? Something like:
public abstract class ABParent {
protected abstract void thingThatsDifferent();
}
#Category(CategoryA.class)
public class A extends ABParent {
#Override
protected void thingThatsDifferent() {
System.out.println("Woof");
}
}
#Category(CategoryB.class)
public class B extends ABParent {
#Override
protected void thingThatsDifferent() {
System.out.println("Meow");
}
}

Related

Java custom warnings

Hello I wanted to throw a custom warning. Say i have
public abstract class A {
public void doSomething() {
//base functionality
}
}
public class B extends A {
#Override
public void doSomething() {
super.doSomething();
//extended functionality
}
}
public class C extends A {
}
i want to throw an error for class C because it does not override the method. Normally an abstract method would suffice but the method contains functionality common to all subclass and if there is a better standard or structure please let me know.
I want to force all subclasses to #Override that certain method
I have looked into creating a custom annotation like
public #interface ShouldOverride
but i could not figure out how to throw a warning with that.
Note. I am using IntelliJ Idea Ultimate
Implementation of the proposition of Thomas:
abstract class A {
public void doSomething() {
// basic things to do
// then call implementation
doSomethingImpl();
}
protected abstract void doSomethingImpl();
}
class B extends A {
#Override
protected void doSomethingImpl() {
//extended functionality
}
}
class C extends A { // does not compile
}

Why Class can not extends Interface in java? [closed]

interface a{
public void getSum();
}
public class b implements a{
public void getDivision(){
}
}
We know that but
why class b can not extends interface a ?
What's the reason?
Why can class B not extend the interface A? What's the reason?
The reason is that inheriting from classes (extending them), and implementing interfaces are two different concepts.
Because they are two different concepts, they have different keywords (implements and extends).
Extending a class (Inheritance)
When you extend a class, you inherit all of its fields and methods.
For example:
public class A {
void runInheritedMethod() {
System.out.println("Hello");
}
}
public class B extends A {
}
public class Main {
public static void main(String[] args) {
// This is valid code...
B bInstance = new B();
bInstance.runInheritedMethod();
}
}
Implementing an interface
When you Implement an interface, your class will contain "bodies" or "implementations" for all of the unimplemented functions of said interface.
For example:
public interface Choppable {
public void chop();
}
public class Wood implements Choppable {
public void chop() {
// Your wood-specific code here...
}
}
public class Carrot implements Choppable {
public void chop() {
// Your carrot-specific code here...
}
}
Both of these concepts can be used to achieve polymorphic behaviour, which is why they can cause confusion.

Java - extending the whole class hierarchy

I have a class hierarchy like that:
abstract class BaseThing
{
public abstract void doSomething();
}
class Thing1 extends BaseThing
{
#Override
public void doSomething()
{
doSomethingWithThing1();
}
}
class Thing2 extends BaseThing
{
#Override
public void doSomething()
{
doSomethingWithThing2();
}
}
// A dozen more classes that extend the BaseThing class.
I need to create an extended version of the whole tree. There is no multiple inheritance in Java, so I created it as:
interface BaseThingExt
{
public void doSomethingElse();
}
class Thing1Ext extends Thing1 implements BaseThingExt
{
#Override
public void doSomethingElse()
{
doSomethingElseWithThing1();
}
}
// All Thing.. classes are extended by ThingExt... classes
Now the question. Where can I put some common fields for all ThingExt classes? I cannot place them in the base interface, as they would become final static. I cannot make BaseThingExt an abstract class as Java doesn't support a multiple inheritance. I cannot believe the only solution is to replicate them a dozen times in all ThingExt classes!
EDIT: Please note that all ThingExt classes should extend their according Thing classes and not just the BaseThing class, because there are some specifics in each derived class. So the answer by #Hamdi Douss won't work.
The usual trick is to use composition rather than inheritance:
interface BaseThingExt
{
public void doSomethingElse();
}
class ConcreteImplementation implements BaseThing, BaseThingExt {
private final BaseThing thingDelegate;
private final BaseThingExt extDelegate;
public ConcreteImplementation(BaseThing thing, BaseThingExt ext) {
this.thingDelegate = thing;
this.extDelegate = ext;
}
#Override
public void doSomething() {
thingDelegate.doSomething();
}
#Override
public void doSomethingElse() {
extDelegate.doSomethingElse();
}
}
I suggest to add a super class AbstractBaseThingExt:
abstract class AbstractBaseThingExt implements BaseThingExt
{
private Object commonField;
public Object getCommonField(){}
public Object setCommonField(Object commonField){}
}
class ThingExt extends AbstractBaseThingExt
{
public ThingExt(BaseThing base) {
this.base = base;
}
public void doSomething()
{
this.base.doSomething();
}
}
The class ThingExt should delegate implementation to base when appropriate.

Extending an abstract class - implementing abstract methods

Greetings and salutations!
I currently have an abstract class A, and many classes subclassing it. The code common to all the subclasses I've put in the oneMethod() and the code that's specific to each implementation I've put into two abstract methods.
public abstract class AbstractA {
public oneMethod() {
//do some intelligent stuff here
abstractMethodOne();
abstractMethodTwo();
}
protected abstract void abstractMethodOne();
protected abstract void abstractMethodTwo();
}
I have a class that overrides the oneMethod() method.
public class B extends AbstractA {
#Override
public oneMethod() {
//do some other intelligent stuff here
}
}
is there any way to skip making a stub implementation of the two abstract methods in the subclass? I mean the only place they're used is in the overridden method.
Any help is appreciated!
You have to provide an implementation to all abstract methods. Even if no part of the program calls them now a class can be created in the future that does call them, or the super class implementation may be changed. A stub is needed even if it's just for binary compatibility.
No. If you extend an abstract class, you must either make the child class abstract or it must fulfill the contract of the parent class.
As a design observation, I would suggest that you try to make oneMethod() either final or abstract. It's hard to maintain programs that allow extension the way you're implementing it. Use other abstract methods to give child classes hooks into the functionality of oneMethod().
You could pull oneMethod up into a superclass:
public abstract class AbstractC {
public void oneMethod() {
}
}
public abstract class AbstractA extends AbstractC {
#Override
public void oneMethod() {
//do some intelligent stuff here
abstractMethodOne();
abstractMethodTwo();
}
protected abstract void abstractMethodOne();
protected abstract void abstractMethodTwo();
}
public class B extends AbstractC {
#Override
public void oneMethod() {
//do some other intelligent stuff here
}
}
see now how you don't need any more in AbstractC than you need.
Just make class B also abstract.
public abstract class B extends AbstractA {
Since abstractMethodOne() and abstractMethodTwo() are implementation specific but you know that you will always call them you can use composition like this:
public interface SomeInterface {
void abstractMethodOne();
void abstractMethodTwo();
}
and create a class like this:
public class SomeClass {
public void executeThem(SomeInterface onSomeObject) {
onSomeObject.abstractMethodOne();
onSomeObject.abstractMethodTwo();
}
}
then you can compose this in any of your classes where you should call those methods like this:
public class SomeImplementation implements SomeInterface {
public void abstractMethodOne() {
// ...
}
public void abstractMethodTwo() {
// ...
}
public void executeThem() {
new SomeClass().executeThem(this);
}
}
This way you got rid of the inheritance altogether and you can be more flexible in your classes implementing SomeInterface.
If your classes B and A have to implement their own oneMethod it's maybe because there are not in an inheritance link but they just should implement the same interface ?
Well, if abstractMethodTwo and abstractMethodOne are implementation specific, why you put these methods in the base abstract class ? Maybe a common interface or some specific design-pattern is what you're looking for!

When to use abstract classes and when to use interfaces [duplicate]

Possible Duplicate:
When to use interfaces or abstract classes? When to use both?
Consider this
public abstract class Flat
{
//some properties
public void livingRoom(){
//some code
}
public void kitchen(){
//some code
}
public abstract void bedRoom();
}
An implementation class would be as follows:
public class Flat101 extends Flat
{
public void bedRoom() {
System.out.println("This flat has a customized bedroom");
}
}
Alternatively I can use an interface instead of an abstract class to achieve the same purpose like follows:
class Flat
{
public void livingRoom(){
System.out.println("This flat has a living room");
}
public void kitchen(){
System.out.println("This flat has a kitchen");
}
}
interface BedRoomInterface
{
public abstract void bedRoom();
}
public class Flat101 extends Flat implements BedRoomInterface
{
public void bedRoom() {
System.out.println("This flat has a customized bedroom");
}
}
Now the question is : For this setup why should choose to use an interface (or) why should I choose to use an abstract class?
Generally speaking, use abstract classes when you want a place to put common logic that can be reused between implementations. Otherwise, use an Interface.
Then there are a number of exception, often closely related to design patterns. But keep it simple.

Resources