Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

threefive posted:

If there a Java equivalent to Ruby's ||= operator?

Nope. Best you can do is = (value == null) ? default : value;


Edit: Maybe Guava's Objects.firstNotNull(value, default); is more clear.

Adbot
ADBOT LOVES YOU

Sedro
Dec 31, 2008

Gravity Pike posted:

Edit: Maybe Guava's Objects.firstNotNull(value, default); is more clear.
That will always evaluate default so be careful

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Gravity Pike posted:

Nope. Best you can do is = (value == null) ? default : value;


Edit: Maybe Guava's Objects.firstNotNull(value, default); is more clear.

(value == null) ? default : value = amount;


Ruby's ||= is weird, I'm not even sure where that's all that useful.

Just do if(a || a = b) or if(a == null){a=b;}

Java doesn't always like syntactic shorthand if it causes ambiguity or if its less readable. You can't overload operators, you just write methods instead. Its more verbose, but that can be a good thing.

Zaphod42 fucked around with this message at 16:34 on May 14, 2014

foxy boxing babe
Jan 17, 2010


I'm writing a Tetris clone as I learn Java, and I have this bug where the board will poo poo itself and fill in all the spaces above a piece that just landed instead of just adding the piece. Both the board and the pieces are represented by a 2D array of booleans. The code for adding a piece to the board is just a loop that checks which elements of the piece are true and sets the corresponding elements of the board true.
code:
for (int row = 0; row < crt.length; row++)
{
    for (int col = 0; col < crt[row].length; col++)
    {
       if (crt[row][col])
       {
           board[row + t.getY()][col + t.getX()] = true;
       }
    }
}
Some testing I did showed that it does break here, but I can't find a pattern and the code seems really simple. When I clone the board array instead of acting on the board directly, the problem disappears entirely, but I don't know why. Does anyone have any ideas?

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
Do you have anything that clears out the tiles that a piece moves out of?

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Julie And Candy posted:

I'm writing a Tetris clone as I learn Java, and I have this bug where the board will poo poo itself and fill in all the spaces above a piece that just landed instead of just adding the piece. Both the board and the pieces are represented by a 2D array of booleans. The code for adding a piece to the board is just a loop that checks which elements of the piece are true and sets the corresponding elements of the board true.

Some testing I did showed that it does break here, but I can't find a pattern and the code seems really simple. When I clone the board array instead of acting on the board directly, the problem disappears entirely, but I don't know why. Does anyone have any ideas?

Tetris is a classic problem to implement, good stuff.

That code's fine as far as I can see. But if you mention cloning the board array makes the problem go away, that's extremely key. Where are you cloning the board array or not cloning it? That's the code we need to see, that's how the bug is happening, I wager. Maybe dump it in postbin and link it to us if there's too much code?

Are both arrays using the same orientation (increasing Y = Up or Down?), although that shouldn't fill everything...

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
What is going on? Netbeans transformed pretty straightforward
code:
        for (Exercise e : exercises) {
            e.getCategories().retainAll(categories);
            if (e.getCategories().isEmpty()) {
                e.getCategories().add(DEFAULT_CATEGORY);
            }
        }
into
code:
        exercises.stream().map((e) -> {
            e.getCategories().retainAll(categories);
            return e;
        }).filter((e) -> (e.getCategories().isEmpty())).forEach((e) -> {
            e.getCategories().add(DEFAULT_CATEGORY);
        });
Is it even valid? I'm kind of trying to learn functional operations by example, and I've been bearing stream.forEach rather well, but that... Am I wrong in thinking that it's harder to read than the original? (I've just studied the javadocs for all of the components, and it does appear to be valid.)

supermikhail fucked around with this message at 16:03 on May 17, 2014

Gul Banana
Nov 28, 2003

that seems like an unnecessary transformation for sure. particularly as you don't have a real streaming situation here there's nothing to be gained by the increase in complexity.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Oh, for folks not using NetBeans I should probably mention that it was just a hint that the transformation was available, not a pressing suggestion on the part of the IDE that I should do it.:) Although what's supposed to be an appropriate streaming situation? Netbeans suggests this for almost any loop, it appears.

pliable
Sep 26, 2003

this is what u get for "180 x 180 avatars"

this is what u fucking get u bithc
Fun Shoe
This is part of my ongoing confusion with Java's pass-by-value/reference dealio:

Say I have an ArrayList of some object, and say that object implements Comparable so it can be sorted via Collections.sort(). Why the gently caress does that even work, if Java passes by value? For example:

code:
ArrayList<myObject> myObjects = new ArrayList<myObject>();

//code to populate myObjects

Collections.sort(myObjects);

//now my poo poo is sorted hooray!
Why does this work? Thanks y'all <3

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

The "value" of any object variable is really its reference. So the value of myObjects is unchanged, as it still points to the same object on the heap, but you gave the sort() method that reference. The difference between this and true pass-by-reference is that under pass by reference, sort() could actually change the address that myObjects pass to, which is impossible in Java.

e: it's a very subtle distinction, but it's one worth making.

pliable
Sep 26, 2003

this is what u get for "180 x 180 avatars"

this is what u fucking get u bithc
Fun Shoe

carry on then posted:

e: it's a very subtle distinction, but it's one worth making.

Yeah, and its subtlety still trips me up. But awesome, that clarifies...thank you much!

ulmont
Sep 15, 2010

IF I EVER MISS VOTING IN AN ELECTION (EVEN AMERICAN IDOL) ,OR HAVE UNPAID PARKING TICKETS, PLEASE TAKE AWAY MY FRANCHISE

pliable posted:

This is part of my ongoing confusion with Java's pass-by-value/reference dealio:

Ok. For Java, consider it to be pass by reference for anything that isn't a primitive type. Technically, everything is pass by value...but Objects are only accessed through references, so the reference is passed by value, and then YOLO.

pliable posted:

Why does this work? Thanks y'all <3

It works because you are accessing the ArrayList through a reference (or a value copy thereof).

That may not have helped; look here:
http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

e;f,b.

Utterly Irrelephant
Sep 6, 2007
ive got a rocket in my pocket
I am taking a class in java and I have a few questions (I am not asking for homework help)

Background I have openJDK 7 installed on debian and I have a slightly older version of eclipse installed from the repos. The teacher had everyone install java 1.8 JDK and the new windows version of Eclipse(not in the debian repos). My question is: Has enough changed that I should bother trying to code in a virtualBox XP install with all the standard software, or will I probably be fine coding with openJDK-7 and an older version of eclipse? Should I try to code java in VIM instead of eclipse? Anything I should know going ahead?

P.S. The prof doesn't care which IDE or text editor I use, however I believe most professors in the CS department use eclipse with java.

Utterly Irrelephant fucked around with this message at 15:18 on May 21, 2014

Woodsy Owl
Oct 27, 2004

Egorger Le Chef posted:

I am taking a class in java and I have a few questions (I am not asking for homework help)

Background I have openJDK 7 installed on debian and I have a slightly older version of eclipse installed from the repos. The teacher had everyone install java 1.8 JDK and the new windows version of Eclipse(not in the debian repos). My question is: Has enough changed that I should bother trying to code in a virtualBox XP install with all the standard software, or will I probably be fine coding with openJDK-7 and an older version of eclipse? Should I try to code java in VIM instead of eclipse? Anything I should know going ahead?

P.S. The prof doesn't care which IDE or text editor I use, however I believe most professors in the CS department use eclipse with java.

Chances are that you won't be learning and implementing the new features of Java 8 (1.8) over 7 (1.7) in your intro class. If the professors use Eclipse then it's probably advantageous to use it too. The newest version of Eclipse is pretty great. For the sake of being able to focus on coding instead of project setup and IDE overhead, instantiate a new XP VM and have at it. It might save you some headache in the future.

But, if you want a few added challenges then just go for 7 and VIM.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Egorger Le Chef posted:

I am taking a class in java and I have a few questions (I am not asking for homework help)

Background I have openJDK 7 installed on debian and I have a slightly older version of eclipse installed from the repos. The teacher had everyone install java 1.8 JDK and the new windows version of Eclipse(not in the debian repos). My question is: Has enough changed that I should bother trying to code in a virtualBox XP install with all the standard software, or will I probably be fine coding with openJDK-7 and an older version of eclipse? Should I try to code java in VIM instead of eclipse? Anything I should know going ahead?

P.S. The prof doesn't care which IDE or text editor I use, however I believe most professors in the CS department use eclipse with java.

I thinking setting up XP with a VM is a waste of time and won't gain you anything, you can install the latest JVM and Eclipse in your Debian machine and not notice the difference with the Windows version. I also don't think that you'll be using the new features of Java 8, so you can probably stick with openjdk7. However I do recommend you use Eclipse for your IDE, but it really doesn't matter what version of Eclipse you use. Also don't code Java in VIM, it will just force more complexity when trying to run your code.

Withnail
Feb 11, 2004
Can anyone recommend a tool to automate the documentation of jaxb Rest web services (we run on jboss and build with ant)? We are playing with enunciate, but keep running into issues.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Withnail posted:

Can anyone recommend a tool to automate the documentation of jaxb Rest web services (we run on jboss and build with ant)? We are playing with enunciate, but keep running into issues.

I would actually recommend enunciate :downs: Its the only one I've worked with though. It did take me some finagling to get it to output right, I do remember, but the results were worth it, very nice. If you build with ant already it shouldn't be all that hard... what issues?

Withnail
Feb 11, 2004
Not fully sure, I'm not in the trenches on this one. I was going to see if there was something super great that everyone else is using. I'll have the developer fight with it some more.

pliable
Sep 26, 2003

this is what u get for "180 x 180 avatars"

this is what u fucking get u bithc
Fun Shoe

Egorger Le Chef posted:

I am taking a class in java and I have a few questions (I am not asking for homework help)

Background I have openJDK 7 installed on debian and I have a slightly older version of eclipse installed from the repos. The teacher had everyone install java 1.8 JDK and the new windows version of Eclipse(not in the debian repos). My question is: Has enough changed that I should bother trying to code in a virtualBox XP install with all the standard software, or will I probably be fine coding with openJDK-7 and an older version of eclipse? Should I try to code java in VIM instead of eclipse? Anything I should know going ahead?

P.S. The prof doesn't care which IDE or text editor I use, however I believe most professors in the CS department use eclipse with java.

Yeah like others said, one of the new features of Java 8 is closures, which, would be impressive if they ended up teaching it (I didn't even learn about closures until my upper division courses, so...).

And call me spoiled but I can't program in Java without eclipse. There are too many useful features that make programming in Java much easier (eg: automatic building, code completion, etc). I only use Vim when programming in C (and sometimes Python, but I mostly use PyCharm for Python).


Thanks for the help anyway :)

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

pliable posted:

And call me spoiled but I can't program in Java without eclipse. There are too many useful features that make programming in Java much easier (eg: automatic building, code completion, etc). I only use Vim when programming in C (and sometimes Python, but I mostly use PyCharm for Python).

You don't have to use Eclipse, but in 2014 you do have to use an IDE. Vi and Emacs aren't IDEs.

pliable
Sep 26, 2003

this is what u get for "180 x 180 avatars"

this is what u fucking get u bithc
Fun Shoe

Zaphod42 posted:

You don't have to use Eclipse, but in 2014 you do have to use an IDE. Vi and Emacs aren't IDEs.

If you're really hardcore, you can use a simple text editor. I don't recommend it at all, but it's certainly possible and I've seen it done. I have a buddy that uses Vim for all his Python, and simply utilizes Vim tabs and splits to look at everything.

You don't *have* to use an IDE, but it makes life much, MUCH easier. I'm also well aware that Vim and Emacs aren't IDEs ;).

salisbury shake
Dec 27, 2011
Honestly, if you're still learning, it can't hurt to setup a VM and replicate their environment. You can pull the whole Debian thing once you're sure you won't fall behind because your environment is slightly different.

Disclosure: watched a few C++ neckbeards fall behind in a Java course a few years ago because they'd spend their time trying to get poo poo to compile that Eclipse would have caught as they were entering code. When it came time to interface with a DB daemon they were lost and had to use Windows anyway.

Using a plain text editor is needlessly complicating things, imo.

Futuresight
Oct 11, 2012

IT'S ALL TURNED TO SHIT!
In my further adventures into Java I've been working on a very rough and ready maze to figure out GUI stuff and I'm now running into deadlocks and I have no idea what I have to do to fix them. I did the whole wrap the insides of the main function in invokeLater thing, but I'm still running into occasional deadlocks while moving about the maze (through onClick events).

When debugging in Eclipse, if I pause the program normally I get something like this:

code:
Thread [AWT-EventQueue-0] (Suspended)	
	Unsafe.park(boolean, long) line: not available [native method]	
	LockSupport.park(Object) line: not available	
	AbstractQueuedSynchronizer$ConditionObject.await() line: not available	
	EventQueue.getNextEvent() line: not available	
	EventDispatchThread.pumpOneEventForFilters(int) line: not available	
	EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: not available	
	EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available	
	EventDispatchThread.pumpEvents(int, Conditional) line: not available	
	EventDispatchThread.pumpEvents(Conditional) line: not available	
	EventDispatchThread.run() line: not available	
When it seizes up I get a stack like this:

code:
Thread [AWT-EventQueue-0] (Suspended)	
	Maze.getTilePosition(Tile) line: 283	
	Maze.isMoveable(TilePosition, TilePosition) line: 104	
	Game.canMove(TilePosition) line: 63	
	Game.sendHoverEvent(TilePosition) line: 39
	ListenLabel.mouseMoved(MouseEvent) line: 32	
	ListenLabel(Component).processMouseMotionEvent(MouseEvent) line: not available	
	ListenLabel(JComponent).processMouseMotionEvent(MouseEvent) line: not available	
	ListenLabel(Component).processEvent(AWTEvent) line: not available	
	ListenLabel(Container).processEvent(AWTEvent) line: not available	
	ListenLabel(Component).dispatchEventImpl(AWTEvent) line: not available	
	ListenLabel(Container).dispatchEventImpl(AWTEvent) line: not available	
	ListenLabel(Component).dispatchEvent(AWTEvent) line: not available	
	LightweightDispatcher.retargetMouseEvent(Component, int, MouseEvent) line: not available	
	LightweightDispatcher.processMouseEvent(MouseEvent) line: not available	
	LightweightDispatcher.dispatchEvent(AWTEvent) line: not available	
	JFrame(Container).dispatchEventImpl(AWTEvent) line: not available	
	JFrame(Window).dispatchEventImpl(AWTEvent) line: not available	
	JFrame(Component).dispatchEvent(AWTEvent) line: not available	
	EventQueue.dispatchEventImpl(AWTEvent, Object) line: not available	
	EventQueue.access$200(EventQueue, AWTEvent, Object) line: not available	
	EventQueue$3.run() line: not available	
	EventQueue$3.run() line: not available	
	AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]	
	ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: not available	
	ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext) line: not available	
	EventQueue$4.run() line: not available	
	EventQueue$4.run() line: not available	
	AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]	
	ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: not available	
	EventQueue.dispatchEvent(AWTEvent) line: not available	
	EventDispatchThread.pumpOneEventForFilters(int) line: not available	
	EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: not available	
	EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available	
	EventDispatchThread.pumpEvents(int, Conditional) line: not available	
	EventDispatchThread.pumpEvents(Conditional) line: not available	
	EventDispatchThread.run() line: not available
Everything after and including the first ListenLabel line in the second block is common across the tests I did.

What sort of things do I have to do in order to avoid this? I have a MouseInputListener, I change the position of 2 things (the player onClick, and a translucent square that uses onHover to highlight the tile the mouse is over), and change the color of 1 thing (the highlight). I'm using setBounds to move the stuff around. There's no game loop or anything, it's all listeners making it go.

Utterly Irrelephant
Sep 6, 2007
ive got a rocket in my pocket
Java student here again.

Turns out java 8 doesn't support XP anyways and 7 seams fine. I think I'm going to use eclipse 3.8 for now with openJDK on Debian Sid. A girl in my class was having more trouble running eclipse on her macbook (we couldn't find where to create a new class in the project) so I guess I got off easy.

I guess the only reason I was thinking VIM was because I've heard it is a great piece of software to become familiar with. Thanks for the input, can't wait until I have some knowledge/experience to contribute back.

HFX
Nov 29, 2004

Egorger Le Chef posted:

Java student here again.

Turns out java 8 doesn't support XP anyways and 7 seams fine. I think I'm going to use eclipse 3.8 for now with openJDK on Debian Sid. A girl in my class was having more trouble running eclipse on her macbook (we couldn't find where to create a new class in the project) so I guess I got off easy.

I guess the only reason I was thinking VIM was because I've heard it is a great piece of software to become familiar with. Thanks for the input, can't wait until I have some knowledge/experience to contribute back.

Java 8 actually runs on Windows XP just fine. However, since Microsoft is dropping support so is Oracle. Instructions: http://inbytebg.com/techblog/?p=189

For new class: https://www.youtube.com/watch?v=t1Phk5BfqZI

Don't bother learning Java 8. It is great, but any enterprise Java job you get will probably be just moving to Java 7 by the time you graduate. I only kid slightly. There isn't enough difference in Java 7 and Java 8 syntactically for you to worry about right now though! So have fun, go wild.

HFX fucked around with this message at 01:50 on May 22, 2014

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

I'd say if you're curious about what a new Java release generally adds, take a quick look at Java 8, but don't think you'll find any major revelation. Lambdas are a neat space saver when doing ActionListeners and Runnables but if you don't use those/know what those are then you definitely won't find it useful.

Eclipse I would recommend installing the latest version if you have some time; while I'm not an Eclipse expert I do know that 4.3 is probably better than 3.8, and installing it, at least on Ubuntu, isn't too big of a hassle.

You really shouldn't have to install Windows XP in a VM to follow your class, I learned programming in Java on OS X with no issue. Other, more platform dependent languages will be a different story, however.

Utterly Irrelephant
Sep 6, 2007
ive got a rocket in my pocket

HFX posted:

Java 8 actually runs on Windows XP just fine. However, since Microsoft is dropping support so is Oracle. Instructions: http://inbytebg.com/techblog/?p=189

For new class: https://www.youtube.com/watch?v=t1Phk5BfqZI

Don't bother learning Java 8. It is great, but any enterprise Java job you get will probably be just moving to Java 7 by the time you graduate. I only kid slightly. There isn't enough difference in Java 7 and Java 8 syntactically for you to worry about right now though! So have fun, go wild.

I may try again in XP later if I have problems. That video is how I created a class, but when she right clicked on the project, 90% of the choices weren't there. :iiam:


carry on then posted:

...
Eclipse I would recommend installing the latest version if you have some time; while I'm not an Eclipse expert I do know that 4.3 is probably better than 3.8, and installing it, at least on Ubuntu, isn't too big of a hassle.
...

I'll look into it first time I cant figure out why my code wont compile and I need something to do :bang:

Futuresight
Oct 11, 2012

IT'S ALL TURNED TO SHIT!
Okay I think I see what's up. My implementation breaks out of the event dispatching thread between the mouse events and writing to the GUI, so I need to schedule and send to the event dispatching thread every GUI update that does this. Now to actually try the fix.

EDIT: Or it's a loving bug in the maze class. God loving damnit I should have checked the basic poo poo before going off on this thread tangent. Oh well, I guess I learned more about Java on the way so... nope still pissed at myself.

Futuresight fucked around with this message at 04:10 on May 22, 2014

HFX
Nov 29, 2004
Question regarding unit testing to the more experienced maven users.
We have some code quality requirements like 80% of all code must be covered under unit tests which are picked up by an automated scanning tool.

I am using maven to keep my test cases separate from my code. The issue comes in when I want to test the method I just created that does something like pull an item from a map, create a new JAXB object to hold the item, then return the JAXB object. This should be perfectly unit testable. My problem because that I would like to make the method private. However, in order to have a unit test that just deals with that one function, I need to make it at least package wide. Is there a better way to do this?

Sedro
Dec 31, 2008
You wouldn't usually test the private method, you would test whatever public method uses it (which would also satisfy code coverage). But if you still want to test the private method, you are correct-- put the method in package scope and create a test in the same package.

HFX
Nov 29, 2004

Sedro posted:

You wouldn't usually test the private method, you would test whatever public method uses it (which would also satisfy code coverage). But if you still want to test the private method, you are correct-- put the method in package scope and create a test in the same package.

Yep. The problem I have is testing the public method in such a manner becomes unwieldy once I begin talking about converting a large document with many fields into their correct places in an XML document.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Is it an incredibly bad idea to create a nested class in your test class, which extends the class you're testing and adds public methods which call the private ones? That way you don't need to change any access modifiers and you can keep all the testing gubbins separate from the real code

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

HFX posted:

Yep. The problem I have is testing the public method in such a manner becomes unwieldy once I begin talking about converting a large document with many fields into their correct places in an XML document.

I obviously don't know your specific application, but this has a bit of "code smell" about it. Designing things to be testable is part off design: if a part that you're interested in unit-testing isn't publicly visible, it might indicate that you have insufficient or incorrect encapsulation. Dependency injection isn't just for spring beans.

And, as I'm sure you're aware (and that your manager isn't), code coverage metrics are a tool, not a target. In an ideal world, you'd write good tests, and then use coverage metrics to discover where you'd missed something. In practice, the number and uniqueness of assertions, not code coverage, are what make for well tested code.

HFX
Nov 29, 2004

Gravity Pike posted:

I obviously don't know your specific application, but this has a bit of "code smell" about it. Designing things to be testable is part off design: if a part that you're interested in unit-testing isn't publicly visible, it might indicate that you have insufficient or incorrect encapsulation. Dependency injection isn't just for spring beans.

And, as I'm sure you're aware (and that your manager isn't), code coverage metrics are a tool, not a target. In an ideal world, you'd write good tests, and then use coverage metrics to discover where you'd missed something. In practice, the number and uniqueness of assertions, not code coverage, are what make for well tested code.

Think about a system where you want information about a consumer product. You want to know everything about that product including each parts material, costs, taxes, etc. Now you need to map this into a huge XML document that gets passed back and forth. Rather then trying to test all the possible paths that could be created, I'm trying to test just a couple elements at a time.

As to code coverage metrics, I am well aware and am trying to push back constantly on their blanket code coverage / quality items. However, since I don't have a big title like enterprise architect behind my name, no one takes me serious even if I put articles in front of them.

Sedro posted:

You wouldn't be able to call the private methods, and protected wouldn't really be better than package scope if you actually wanted them to be private.

I normally don't forward reference, but this is a concise explanation.
Ask me how many times I got bit by this when switching off a C++ project and onto a Java project in my first few years.

HFX fucked around with this message at 17:40 on May 22, 2014

Sedro
Dec 31, 2008

baka kaba posted:

Is it an incredibly bad idea to create a nested class in your test class, which extends the class you're testing and adds public methods which call the private ones? That way you don't need to change any access modifiers and you can keep all the testing gubbins separate from the real code
You wouldn't be able to call the private methods, and protected wouldn't really be better than package scope if you actually wanted them to be private.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah my mistake, I was thinking of protected methods and variables. Sorry, don't mind me, I've been writing unit tests all week and debugging at the same time, it's leaking out

FateFree
Nov 14, 2003

Never change your level of visibility for something like unit testing. If a method is private, its private for a reason, to hide implementation details of your code. Its perfectly valid to want to test private methods in isolation as well. Honestly I'd just use reflection to test the method.

pigdog
Apr 23, 2004

by Smythe

baka kaba posted:

Is it an incredibly bad idea to create a nested class in your test class, which extends the class you're testing and adds public methods which call the private ones? That way you don't need to change any access modifiers and you can keep all the testing gubbins separate from the real code

It is a useful technique, but as mentioned, you still wouldn't get access to the classes private members.

Most of the time if you feel the urge to unit test private methods in a class, and the public interface for that class won't do, then that signifies that you'd be better off putting that code in an separate utility class of some sort and testing it properly in isolation.

Adbot
ADBOT LOVES YOU

PleasantDilemma
Dec 5, 2006

The Last Hope for Peace
Newbie here. I'm using Maven and NetBeans 7.4. How do I get maven to copy some xml & properties files from src to target when I run `mvn package`? The files are in src/main/java/com/mycompany/reports/config. When I run the jar it keeps dying saying it cant find the files in target/classes/com/mycompany/reports/config. I've been surfing the maven site for a while but I'm not seeing what I need to put in my pom to get it to copy the files.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply